File tree Expand file tree Collapse file tree 5 files changed +105
-29
lines changed
solution/2000-2099/2011.Final Value of Variable After Performing Operations Expand file tree Collapse file tree 5 files changed +105
-29
lines changed Original file line number Diff line number Diff line change @@ -97,18 +97,6 @@ class Solution {
97
97
}
98
98
```
99
99
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
-
112
100
### ** C++**
113
101
114
102
``` cpp
@@ -138,6 +126,50 @@ func finalValueAfterOperations(operations []string) int {
138
126
}
139
127
```
140
128
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
+
141
173
### **...**
142
174
143
175
```
Original file line number Diff line number Diff line change @@ -87,18 +87,6 @@ class Solution {
87
87
}
88
88
```
89
89
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
-
102
90
### ** C++**
103
91
104
92
``` cpp
@@ -128,6 +116,50 @@ func finalValueAfterOperations(operations []string) int {
128
116
}
129
117
```
130
118
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
+
131
163
### **...**
132
164
133
165
```
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
1
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 ) ;
7
3
}
You can’t perform that action at this time.
0 commit comments