Skip to content

Commit 7b80ae1

Browse files
committed
feat: add solutions to lc problem: No.2011
No.2011.Final Value of Variable After Performing Operations
1 parent 060e300 commit 7b80ae1

File tree

5 files changed

+105
-29
lines changed

5 files changed

+105
-29
lines changed

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

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,6 @@ class Solution {
9797
}
9898
```
9999

100-
### **TypeScript**
101-
102-
```ts
103-
function finalValueAfterOperations(operations: string[]): number {
104-
let ans = 0;
105-
for (let operation of operations) {
106-
ans += operation.includes('+') ? 1 : -1;
107-
}
108-
return ans;
109-
}
110-
```
111-
112100
### **C++**
113101

114102
```cpp
@@ -138,6 +126,50 @@ func finalValueAfterOperations(operations []string) int {
138126
}
139127
```
140128

129+
### **TypeScript**
130+
131+
```ts
132+
function finalValueAfterOperations(operations: string[]): number {
133+
let ans = 0;
134+
for (let operation of operations) {
135+
ans += operation.includes('+') ? 1 : -1;
136+
}
137+
return ans;
138+
}
139+
```
140+
141+
```ts
142+
function finalValueAfterOperations(operations: string[]): number {
143+
return operations.reduce((r, v) => r + (v[1] === '+' ? 1 : -1), 0);
144+
}
145+
```
146+
147+
### **Rust**
148+
149+
```rust
150+
impl Solution {
151+
pub fn final_value_after_operations(operations: Vec<String>) -> i32 {
152+
let mut ans = 0;
153+
for s in operations.iter() {
154+
ans += if s.as_bytes()[1] == b'+' { 1 } else { -1 };
155+
}
156+
ans
157+
}
158+
}
159+
```
160+
161+
### **C**
162+
163+
```c
164+
int finalValueAfterOperations(char **operations, int operationsSize) {
165+
int ans = 0;
166+
for (int i = 0; i < operationsSize; i++) {
167+
ans += operations[i][1] == '+' ? 1 : -1;
168+
}
169+
return ans;
170+
}
171+
```
172+
141173
### **...**
142174
143175
```

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

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,6 @@ class Solution {
8787
}
8888
```
8989

90-
### **TypeScript**
91-
92-
```ts
93-
function finalValueAfterOperations(operations: string[]): number {
94-
let ans = 0;
95-
for (let operation of operations) {
96-
ans += operation.includes('+') ? 1 : -1;
97-
}
98-
return ans;
99-
}
100-
```
101-
10290
### **C++**
10391

10492
```cpp
@@ -128,6 +116,50 @@ func finalValueAfterOperations(operations []string) int {
128116
}
129117
```
130118

119+
### **TypeScript**
120+
121+
```ts
122+
function finalValueAfterOperations(operations: string[]): number {
123+
let ans = 0;
124+
for (let operation of operations) {
125+
ans += operation.includes('+') ? 1 : -1;
126+
}
127+
return ans;
128+
}
129+
```
130+
131+
```ts
132+
function finalValueAfterOperations(operations: string[]): number {
133+
return operations.reduce((r, v) => r + (v[1] === '+' ? 1 : -1), 0);
134+
}
135+
```
136+
137+
### **Rust**
138+
139+
```rust
140+
impl Solution {
141+
pub fn final_value_after_operations(operations: Vec<String>) -> i32 {
142+
let mut ans = 0;
143+
for s in operations.iter() {
144+
ans += if s.as_bytes()[1] == b'+' { 1 } else { -1 };
145+
}
146+
ans
147+
}
148+
}
149+
```
150+
151+
### **C**
152+
153+
```c
154+
int finalValueAfterOperations(char **operations, int operationsSize) {
155+
int ans = 0;
156+
for (int i = 0; i < operationsSize; i++) {
157+
ans += operations[i][1] == '+' ? 1 : -1;
158+
}
159+
return ans;
160+
}
161+
```
162+
131163
### **...**
132164
133165
```
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
int finalValueAfterOperations(char **operations, int operationsSize) {
2+
int ans = 0;
3+
for (int i = 0; i < operationsSize; i++) {
4+
ans += operations[i][1] == '+' ? 1 : -1;
5+
}
6+
return ans;
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
impl Solution {
2+
pub fn final_value_after_operations(operations: Vec<String>) -> i32 {
3+
let mut ans = 0;
4+
for s in operations.iter() {
5+
ans += if s.as_bytes()[1] == b'+' { 1 } else { -1 };
6+
}
7+
ans
8+
}
9+
}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
function finalValueAfterOperations(operations: string[]): number {
2-
let ans = 0;
3-
for (let operation of operations) {
4-
ans += operation.includes('+') ? 1 : -1;
5-
}
6-
return ans;
2+
return operations.reduce((r, v) => r + (v[1] === '+' ? 1 : -1), 0);
73
}

0 commit comments

Comments
 (0)