File tree 8 files changed +86
-40
lines changed
2000-2099/2011.Final Value of Variable After Performing Operations
8 files changed +86
-40
lines changed Original file line number Diff line number Diff line change @@ -69,6 +69,12 @@ X--:X 减 1 ,X = 1 - 1 = 0
69
69
70
70
<!-- 这里可写通用的实现逻辑 -->
71
71
72
+ ** 方法一:模拟**
73
+
74
+ 遍历数组 ` operations ` ,对于每个操作 $operations[ i] $,如果包含 ` '+' ` ,那么答案加 $1$,否则答案减 $1$。
75
+
76
+ 时间复杂度为 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 ` operations ` 的长度。
77
+
72
78
<!-- tabs:start -->
73
79
74
80
### ** Python3**
@@ -89,7 +95,7 @@ class Solution:
89
95
class Solution {
90
96
public int finalValueAfterOperations (String [] operations ) {
91
97
int ans = 0 ;
92
- for (String s : operations) {
98
+ for (var s : operations) {
93
99
ans += (s. charAt(1 ) == ' +' ? 1 : - 1 );
94
100
}
95
101
return ans;
@@ -104,7 +110,7 @@ class Solution {
104
110
public:
105
111
int finalValueAfterOperations(vector<string >& operations) {
106
112
int ans = 0;
107
- for (auto s : operations) ans += (s[ 1] == '+' ? 1 : -1);
113
+ for (auto& s : operations) ans += (s[ 1] == '+' ? 1 : -1);
108
114
return ans;
109
115
}
110
116
};
@@ -113,19 +119,34 @@ public:
113
119
### **Go**
114
120
115
121
```go
116
- func finalValueAfterOperations(operations []string) int {
117
- ans := 0
118
- for _, s := range operations {
119
- if s[1] == '+' {
120
- ans += 1
121
- } else {
122
- ans -= 1
123
- }
124
- }
125
- return ans
122
+ func finalValueAfterOperations(operations []string) (ans int) {
123
+ for _, s := range operations {
124
+ if s[1] == '+' {
125
+ ans += 1
126
+ } else {
127
+ ans -= 1
128
+ }
129
+ }
130
+ return
126
131
}
127
132
```
128
133
134
+ ### ** JavaScript**
135
+
136
+ ``` js
137
+ /**
138
+ * @param {string[]} operations
139
+ * @return {number}
140
+ */
141
+ var finalValueAfterOperations = function (operations ) {
142
+ let ans = 0 ;
143
+ for (const s of operations) {
144
+ ans += s[1 ] === ' +' ? 1 : - 1 ;
145
+ }
146
+ return ans;
147
+ };
148
+ ```
149
+
129
150
### ** TypeScript**
130
151
131
152
``` ts
Original file line number Diff line number Diff line change @@ -79,7 +79,7 @@ class Solution:
79
79
class Solution {
80
80
public int finalValueAfterOperations (String [] operations ) {
81
81
int ans = 0 ;
82
- for (String s : operations) {
82
+ for (var s : operations) {
83
83
ans += (s. charAt(1 ) == ' +' ? 1 : - 1 );
84
84
}
85
85
return ans;
@@ -94,7 +94,7 @@ class Solution {
94
94
public:
95
95
int finalValueAfterOperations(vector<string >& operations) {
96
96
int ans = 0;
97
- for (auto s : operations) ans += (s[ 1] == '+' ? 1 : -1);
97
+ for (auto& s : operations) ans += (s[ 1] == '+' ? 1 : -1);
98
98
return ans;
99
99
}
100
100
};
@@ -103,19 +103,34 @@ public:
103
103
### **Go**
104
104
105
105
```go
106
- func finalValueAfterOperations(operations []string) int {
107
- ans := 0
108
- for _, s := range operations {
109
- if s[1] == '+' {
110
- ans += 1
111
- } else {
112
- ans -= 1
113
- }
114
- }
115
- return ans
106
+ func finalValueAfterOperations(operations []string) (ans int) {
107
+ for _, s := range operations {
108
+ if s[1] == '+' {
109
+ ans += 1
110
+ } else {
111
+ ans -= 1
112
+ }
113
+ }
114
+ return
116
115
}
117
116
```
118
117
118
+ ### ** JavaScript**
119
+
120
+ ``` js
121
+ /**
122
+ * @param {string[]} operations
123
+ * @return {number}
124
+ */
125
+ var finalValueAfterOperations = function (operations ) {
126
+ let ans = 0 ;
127
+ for (const s of operations) {
128
+ ans += s[1 ] === ' +' ? 1 : - 1 ;
129
+ }
130
+ return ans;
131
+ };
132
+ ```
133
+
119
134
### ** TypeScript**
120
135
121
136
``` ts
Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ class Solution {
2
2
public:
3
3
int finalValueAfterOperations (vector<string>& operations) {
4
4
int ans = 0 ;
5
- for (auto s : operations) ans += (s[1 ] == ' +' ? 1 : -1 );
5
+ for (auto & s : operations) ans += (s[1 ] == ' +' ? 1 : -1 );
6
6
return ans;
7
7
}
8
8
};
Original file line number Diff line number Diff line change 1
- func finalValueAfterOperations (operations []string ) int {
2
- ans := 0
3
- for _ , s := range operations {
4
- if s [1 ] == '+' {
5
- ans += 1
6
- } else {
7
- ans -= 1
8
- }
9
- }
10
- return ans
1
+ func finalValueAfterOperations (operations []string ) (ans int ) {
2
+ for _ , s := range operations {
3
+ if s [1 ] == '+' {
4
+ ans += 1
5
+ } else {
6
+ ans -= 1
7
+ }
8
+ }
9
+ return
11
10
}
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public int finalValueAfterOperations (String [] operations ) {
3
3
int ans = 0 ;
4
- for (String s : operations ) {
4
+ for (var s : operations ) {
5
5
ans += (s .charAt (1 ) == '+' ? 1 : -1 );
6
6
}
7
7
return ans ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string[] } operations
3
+ * @return {number }
4
+ */
5
+ var finalValueAfterOperations = function ( operations ) {
6
+ let ans = 0 ;
7
+ for ( const s of operations ) {
8
+ ans += s [ 1 ] === '+' ? 1 : - 1 ;
9
+ }
10
+ return ans ;
11
+ } ;
Original file line number Diff line number Diff line change 11
11
12
12
| 段位 | 比例 | 段位名 | 国服分数线 | 勋章 |
13
13
| ----- | ------ | -------- | --------- | --------------------------------------------------------------------------- |
14
- | LV3 | 5% | Guardian | &ge ; 2240.55 | <p ><img alt =" " src =" https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Guardian.gif " style =" width : 80px ;" /></p > |
15
- | LV2 | 20% | Knight | &ge ; 1877.14 | <p ><img alt =" " src =" https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif " style =" width : 80px ;" /></p > |
14
+ | LV3 | 5% | Guardian | &ge ; 2241.57 | <p ><img alt =" " src =" https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Guardian.gif " style =" width : 80px ;" /></p > |
15
+ | LV2 | 20% | Knight | &ge ; 1876.81 | <p ><img alt =" " src =" https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif " style =" width : 80px ;" /></p > |
16
16
| LV1 | 75% | - | - | - |
17
17
18
18
力扣竞赛 ** 全国排名前 10** 的用户,全站用户名展示为品牌橙色。
Original file line number Diff line number Diff line change @@ -127,8 +127,8 @@ def generate_contest_list():
127
127
128
128
| 段位 | 比例 | 段位名 | 国服分数线 | 勋章 |
129
129
| ----- | ------ | -------- | --------- | --------------------------------------------------------------------------- |
130
- | LV3 | 5% | Guardian | ≥2240.55 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Guardian.gif" style="width: 80px;" /></p> |
131
- | LV2 | 20% | Knight | ≥1877.14 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif" style="width: 80px;" /></p> |
130
+ | LV3 | 5% | Guardian | ≥2241.57 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Guardian.gif" style="width: 80px;" /></p> |
131
+ | LV2 | 20% | Knight | ≥1876.81 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif" style="width: 80px;" /></p> |
132
132
| LV1 | 75% | - | - | - |
133
133
134
134
力扣竞赛 **全国排名前 10** 的用户,全站用户名展示为品牌橙色。
You can’t perform that action at this time.
0 commit comments