Skip to content

Commit 86f4cc1

Browse files
committed
feat: add solutions to lc problem: No.2011
No.2011.Final Value of Variable After Performing Operations
1 parent 9668796 commit 86f4cc1

File tree

8 files changed

+86
-40
lines changed

8 files changed

+86
-40
lines changed

solution/2000-2099/2011.Final Value of Variable After Performing Operations/README.md

+33-12
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ X--:X 减 1 ,X = 1 - 1 = 0
6969

7070
<!-- 这里可写通用的实现逻辑 -->
7171

72+
**方法一:模拟**
73+
74+
遍历数组 `operations`,对于每个操作 $operations[i]$,如果包含 `'+'`,那么答案加 $1$,否则答案减 $1$。
75+
76+
时间复杂度为 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `operations` 的长度。
77+
7278
<!-- tabs:start -->
7379

7480
### **Python3**
@@ -89,7 +95,7 @@ class Solution:
8995
class Solution {
9096
public int finalValueAfterOperations(String[] operations) {
9197
int ans = 0;
92-
for (String s : operations) {
98+
for (var s : operations) {
9399
ans += (s.charAt(1) == '+' ? 1 : -1);
94100
}
95101
return ans;
@@ -104,7 +110,7 @@ class Solution {
104110
public:
105111
int finalValueAfterOperations(vector<string>& operations) {
106112
int ans = 0;
107-
for (auto s : operations) ans += (s[1] == '+' ? 1 : -1);
113+
for (auto& s : operations) ans += (s[1] == '+' ? 1 : -1);
108114
return ans;
109115
}
110116
};
@@ -113,19 +119,34 @@ public:
113119
### **Go**
114120
115121
```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
126131
}
127132
```
128133

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+
129150
### **TypeScript**
130151

131152
```ts

solution/2000-2099/2011.Final Value of Variable After Performing Operations/README_EN.md

+27-12
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class Solution:
7979
class Solution {
8080
public int finalValueAfterOperations(String[] operations) {
8181
int ans = 0;
82-
for (String s : operations) {
82+
for (var s : operations) {
8383
ans += (s.charAt(1) == '+' ? 1 : -1);
8484
}
8585
return ans;
@@ -94,7 +94,7 @@ class Solution {
9494
public:
9595
int finalValueAfterOperations(vector<string>& operations) {
9696
int ans = 0;
97-
for (auto s : operations) ans += (s[1] == '+' ? 1 : -1);
97+
for (auto& s : operations) ans += (s[1] == '+' ? 1 : -1);
9898
return ans;
9999
}
100100
};
@@ -103,19 +103,34 @@ public:
103103
### **Go**
104104
105105
```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
116115
}
117116
```
118117

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+
119134
### **TypeScript**
120135

121136
```ts

solution/2000-2099/2011.Final Value of Variable After Performing Operations/Solution.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class Solution {
22
public:
33
int finalValueAfterOperations(vector<string>& operations) {
44
int ans = 0;
5-
for (auto s : operations) ans += (s[1] == '+' ? 1 : -1);
5+
for (auto& s : operations) ans += (s[1] == '+' ? 1 : -1);
66
return ans;
77
}
88
};
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
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
1110
}

solution/2000-2099/2011.Final Value of Variable After Performing Operations/Solution.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution {
22
public int finalValueAfterOperations(String[] operations) {
33
int ans = 0;
4-
for (String s : operations) {
4+
for (var s : operations) {
55
ans += (s.charAt(1) == '+' ? 1 : -1);
66
}
77
return ans;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
};

solution/CONTEST_README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
| 段位 | 比例 | 段位名 | 国服分数线 | 勋章 |
1313
| ----- | ------ | -------- | --------- | --------------------------------------------------------------------------- |
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> |
1616
| LV1 | 75% | - | - | - |
1717

1818
力扣竞赛 **全国排名前 10** 的用户,全站用户名展示为品牌橙色。

solution/contest.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ def generate_contest_list():
127127
128128
| 段位 | 比例 | 段位名 | 国服分数线 | 勋章 |
129129
| ----- | ------ | -------- | --------- | --------------------------------------------------------------------------- |
130-
| 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> |
131-
| 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> |
130+
| 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> |
131+
| 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> |
132132
| LV1 | 75% | - | - | - |
133133
134134
力扣竞赛 **全国排名前 10** 的用户,全站用户名展示为品牌橙色。

0 commit comments

Comments
 (0)