Final Value of Variable After Performing Operations
There is a programming language with only four operations and one variable X:
++XandX++increment the value of the variableXby1.--XandX--decrement the value of the variableXby1.
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.
Example 1
Input
operations = ["--X","X++","X++"]Output
1Starting from 0,
--X makes X equal to -1, and the two X++ operations bring it to 1.Example 2
Input
operations = ["++X","++X","X++"]Output
3Starting from 0, each of the three increment operations increases
X by 1, so the final value is 3.Constraints
- 1 <= operations.length <= 100
- operations[i] will be either "++X", "X++", "--X", or "X--".