Skip to content

Commit b2aacf4

Browse files
committed
update: 添加问题“2011.执行操作后的变量值”的代码(并更新其题解) (#1178)
Signed-off-by: LetMeFly666 <[email protected]>
1 parent 9cd7026 commit b2aacf4

15 files changed

+219
-62
lines changed

.commitmsg

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-10-20 18:44:35
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-10-20 18:46:29
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int finalValueAfterOperations(vector<string>& operations) {
14+
int ans = 0;
15+
for (string& op : operations) {
16+
ans += op[1] == '+' ? 1 : -1;
17+
}
18+
return ans;
19+
}
20+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-10-20 18:44:35
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-10-20 18:48:49
6+
*/
7+
package main
8+
9+
func finalValueAfterOperations(operations []string) (ans int) {
10+
for _, op := range operations {
11+
if op[1] == '+' {
12+
ans++;
13+
} else {
14+
ans--
15+
}
16+
}
17+
return
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-10-20 18:44:35
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-10-20 18:48:23
6+
*/
7+
class Solution {
8+
public int finalValueAfterOperations(String[] operations) {
9+
int ans = 0;
10+
for (String op : operations) {
11+
if (op.charAt(1) == '+') {
12+
ans++;
13+
} else {
14+
ans--;
15+
}
16+
}
17+
return ans;
18+
}
19+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-10-20 18:44:35
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-10-20 18:47:35
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def finalValueAfterOperations(self, operations: List[str]) -> int:
11+
return sum(1 if op[1] == '+' else -1 for op in operations)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-10-20 18:44:35
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-10-20 18:56:38
6+
*/
7+
impl Solution {
8+
pub fn final_value_after_operations(operations: Vec<String>) -> i32 {
9+
let mut ans: i32 = 0;
10+
for op in operations {
11+
if op.as_bytes()[1] == b'+' {
12+
ans += 1; // cannot write as: ans++;
13+
} else {
14+
ans -= 1;
15+
}
16+
}
17+
ans
18+
}
19+
}

Codes/2598-smallest-missing-non-negative-integer-after-operations.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,23 @@
22
* @Author: LetMeFly
33
* @Date: 2025-10-16 19:40:17
44
* @LastEditors: LetMeFly.xyz
5-
* @LastEditTime: 2025-10-16 19:43:19
5+
* @LastEditTime: 2025-10-16 20:23:38
66
*/
77
#if defined(_WIN32) || defined(__APPLE__)
88
#include "_[1,2]toVector.h"
99
#endif
1010

11-
// THIS IS NOT RIGHT
11+
// THIS IS RIGHT
1212
class Solution {
1313
public:
1414
int findSmallestInteger(vector<int>& nums, int value) {
1515
unordered_map<int, int> ma;
1616
for (int t : nums) {
17-
ma[t % value]++;
17+
ma[(t % value + value) % value]++;
1818
}
1919
int ans = 0;
2020
while (true) {
21-
cout << ans << ',' << ma[ans] << endl;
22-
if (--ma[ans] == -1) {
21+
if (--ma[ans % value] == -1) {
2322
return ans;
2423
}
2524
ans++;

Codes/2598-smallest-missing-non-negative-integer-after-operations_right.cpp

Lines changed: 0 additions & 27 deletions
This file was deleted.

Codes/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
* @LastEditTime: 2025-10-11 18:47:55
66
*/
77
pub struct Solution;
8-
include!("2598-smallest-missing-non-negative-integer-after-operations.rs"); // 这个fileName是会被脚本替换掉的
8+
include!("2011-final-value-of-variable-after-performing-operations_20251020.rs"); // 这个fileName是会被脚本替换掉的

Solutions/LeetCode 2011.执行操作后的变量值.md

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
2-
title: 2011.执行操作后的变量值
2+
title: 2011.执行操作后的变量值:简单题简单做
33
date: 2022-12-23 19:19:38
44
tags: [题解, LeetCode, 简单, 数组, 字符串, 模拟]
55
categories: [题解, LeetCode]
66
---
77

8-
# 【LetMeFly】2011.执行操作后的变量值
8+
# 【LetMeFly】2011.执行操作后的变量值:简单题简单做
99

1010
力扣题目链接:[https://leetcode.cn/problems/final-value-of-variable-after-performing-operations/](https://leetcode.cn/problems/final-value-of-variable-after-performing-operations/)
1111

@@ -86,6 +86,9 @@ X--:X 减 1 ,X = 1 - 1 = 0
8686
#### C++
8787

8888
```cpp
89+
/*
90+
* @LastEditTime: 2022-12-23 19:18:09
91+
*/
8992
// 下面代码中,ans即为题解中的X。使用变量ans是一些ACMer的习惯
9093
class Solution {
9194
public:
@@ -102,5 +105,98 @@ public:
102105
};
103106
```
104107
108+
#### C++
109+
110+
```cpp
111+
/*
112+
* @LastEditTime: 2025-10-20 18:46:29
113+
*/
114+
class Solution {
115+
public:
116+
int finalValueAfterOperations(vector<string>& operations) {
117+
int ans = 0;
118+
for (string& op : operations) {
119+
ans += op[1] == '+' ? 1 : -1;
120+
}
121+
return ans;
122+
}
123+
};
124+
```
125+
126+
#### Go
127+
128+
```go
129+
/*
130+
* @LastEditTime: 2025-10-20 18:48:49
131+
*/
132+
package main
133+
134+
func finalValueAfterOperations(operations []string) (ans int) {
135+
for _, op := range operations {
136+
if op[1] == '+' {
137+
ans++;
138+
} else {
139+
ans--
140+
}
141+
}
142+
return
143+
}
144+
```
145+
146+
#### Java
147+
148+
```java
149+
/*
150+
* @LastEditTime: 2025-10-20 18:48:23
151+
*/
152+
class Solution {
153+
public int finalValueAfterOperations(String[] operations) {
154+
int ans = 0;
155+
for (String op : operations) {
156+
if (op.charAt(1) == '+') {
157+
ans++;
158+
} else {
159+
ans--;
160+
}
161+
}
162+
return ans;
163+
}
164+
}
165+
```
166+
167+
#### Python
168+
169+
```python
170+
'''
171+
LastEditTime: 2025-10-20 18:47:35
172+
'''
173+
from typing import List
174+
175+
class Solution:
176+
def finalValueAfterOperations(self, operations: List[str]) -> int:
177+
return sum(1 if op[1] == '+' else -1 for op in operations)
178+
```
179+
180+
#### Rust
181+
182+
```rust
183+
/*
184+
* @LastEditTime: 2025-10-20 18:56:38
185+
*/
186+
impl Solution {
187+
pub fn final_value_after_operations(operations: Vec<String>) -> i32 {
188+
let mut ans: i32 = 0;
189+
for op in operations {
190+
if op.as_bytes()[1] == b'+' {
191+
ans += 1; // cannot write as: ans++;
192+
} else {
193+
ans -= 1;
194+
}
195+
}
196+
ans
197+
}
198+
}
199+
```
200+
105201
> 同步发文于CSDN,原创不易,转载请附上[原文链接](https://blog.letmefly.xyz/2022/12/23/LeetCode%202011.%E6%89%A7%E8%A1%8C%E6%93%8D%E4%BD%9C%E5%90%8E%E7%9A%84%E5%8F%98%E9%87%8F%E5%80%BC/)哦~
106202
> Tisfy:[https://letmefly.blog.csdn.net/article/details/128423159](https://letmefly.blog.csdn.net/article/details/128423159)

0 commit comments

Comments
 (0)