[LeetCode] Final Value of Variable After Performing Operations

2011. Final Value of Variable After Performing Operations

There is a programming language with only four operations and one variable X:

  • ++X and X++ increments the value of the variable X by 1.
  • --X and X-- decrements the value of the variable X by 1.

Initially, the value of X is 0.

Given an array of strings operations containing a list of operations, return the final value of X after performing all the operations.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
int finalValueAfterOperations(vector<string>& operations) {
int res = 0;
for(auto& o : operations) {
if(o[1] == '-') res--;
else res++;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/10/20/PS/LeetCode/final-value-of-variable-after-performing-operations/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.