|
3 | 3 | * Final Value of Variable After Performing Operations
|
4 | 4 | **
|
5 | 5 | * There is a programming language with only four operations and one variable X:
|
6 |
| - * ++X and X++ increments the value of the variable X by 1. |
7 |
| - * --X and X-- decrements the value of the variable X by 1. |
| 6 | + * • ++X and X++ increments the value of the variable X by 1. |
| 7 | + * • --X and X-- decrements the value of the variable X by 1. |
8 | 8 | *
|
9 | 9 | * Initially, the value of X is 0.
|
10 | 10 | * Given an array of strings operations containing a list of operations,
|
11 | 11 | * return the final value of X after performing all the operations.
|
12 |
| - * |
| 12 | + * |
13 | 13 | * Example 1:
|
14 | 14 | * Input: operations = ["--X","X++","X++"]
|
15 | 15 | * Output: 1
|
|
19 | 19 | * X++: X is incremented by 1, X = -1 + 1 = 0.
|
20 | 20 | * X++: X is incremented by 1, X = 0 + 1 = 1.
|
21 | 21 | *
|
22 |
| - * |
23 | 22 | * Example 2:
|
24 | 23 | * Input: operations = ["++X","++X","X++"]
|
25 | 24 | * Output: 3
|
|
29 | 28 | * ++X: X is incremented by 1, X = 1 + 1 = 2.
|
30 | 29 | * X++: X is incremented by 1, X = 2 + 1 = 3.
|
31 | 30 | *
|
32 |
| - * |
33 | 31 | * Example 3:
|
34 | 32 | * Input: operations = ["X++","++X","--X","X--"]
|
35 | 33 | * Output: 0
|
|
39 | 37 | * ++X: X is incremented by 1, X = 1 + 1 = 2.
|
40 | 38 | * --X: X is decremented by 1, X = 2 - 1 = 1.
|
41 | 39 | * X--: X is decremented by 1, X = 1 - 1 = 0.
|
42 |
| - * |
| 40 | + * |
| 41 | + * Constraints: |
| 42 | + * • 1 <= operations.length <= 100 |
| 43 | + * • operations[i] will be either "++X", "X++", "--X", or "X--". |
| 44 | + * |
43 | 45 | * Hint 1: There are only two operations to keep track of.
|
44 | 46 | * Hint 2: Use a variable to store the value after each operation.
|
45 | 47 | **
|
|
0 commit comments