Input: operations =["--X","X++","X++"]Output: 1Explanation: The operations are performed as follows:Initially, X =0.--X: X is decremented by 1, X =0-1=-1.X++: X is incremented by 1, X =-1+1=0.X++: X is incremented by 1, X =0+1=1.
Input: operations =["++X","++X","X++"]Output: 3Explanation: The operations are performed as follows:Initially, X =0.++X: X is incremented by 1, X =0+1=1.++X: X is incremented by 1, X =1+1=2.X++: X is incremented by 1, X =2+1=3.
Input: operations =["X++","++X","--X","X--"]Output: 0Explanation: The operations are performed as follows:Initially, X =0.X++: X is incremented by 1, X =0+1=1.++X: X is incremented by 1, X =1+1=2.--X: X is decremented by 1, X =2-1=1.X--: X is decremented by 1, X =1-1=0.
classSolution {
publicintfinalValueAfterOperations(String[] operations) {
int ans = 0;
for (String op : operations) {
if (op.contains("+")) ans++;
else ans--;
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
classSolution {
funfinalValueAfterOperations(operations: Array<String>): Int {
var ans = 0for (op in operations) {
if ('+'in op) ans++else ans-- }
return ans
}
}
1
2
3
4
5
6
7
8
9
classSolution:
deffinalValueAfterOperations(self, operations: list[str]) -> int:
ans =0for op in operations:
if'+'in op:
ans +=1else:
ans -=1return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
impl Solution {
pubfnfinal_value_after_operations(operations: Vec<String>) -> i32 {
letmut ans =0;
for op in operations {
if op.contains('+') {
ans +=1;
} else {
ans -=1;
}
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
classSolution {
finalValueAfterOperations(operations: string[]):number {
letans=0;
for (constopofoperations) {
if (op.includes('+')) ans++;
elseans--;
}
returnans;
}
}