|
4 | 4 |
|
5 | 5 | There is a programming language with only **four** operations and **one** variable `X`: |
6 | 6 |
|
7 | | -* `++X` and `X++` **increments** the value of the variable `X` by `1`. |
8 | | -* `--X` and `X--` **decrements** the value of the variable `X` by `1`. |
| 7 | +* `++X` and `X++` **increments** the value of the variable `X` by `1`. |
| 8 | +* `--X` and `X--` **decrements** the value of the variable `X` by `1`. |
9 | 9 |
|
10 | 10 | Initially, the value of `X` is `0`. |
11 | 11 |
|
12 | | -Given an array of strings `operations` containing a list of operations, return the final value of `X` *after performing all the operations* . |
| 12 | +Given an array of strings `operations` containing a list of operations, return _the **final** value of_ `X` _after performing all the operations_. |
13 | 13 |
|
14 | 14 | **Example 1:** |
15 | 15 |
|
16 | 16 | **Input:** operations = ["--X","X++","X++"] |
17 | 17 |
|
18 | 18 | **Output:** 1 |
19 | 19 |
|
20 | | -**Explanation:** |
| 20 | +**Explanation:** The operations are performed as follows: |
21 | 21 |
|
22 | | -The operations are performed as follows: |
| 22 | +Initially, X = 0. |
23 | 23 |
|
24 | | -Initially, X = 0. |
| 24 | +--X: X is decremented by 1, X = 0 - 1 = -1. |
25 | 25 |
|
26 | | ---X: X is decremented by 1, X = 0 - 1 = -1. |
| 26 | +X++: X is incremented by 1, X = -1 + 1 = 0. |
27 | 27 |
|
28 | | -X++: X is incremented by 1, X = -1 + 1 = 0. |
29 | | - |
30 | | -X++: X is incremented by 1, X = 0 + 1 = 1. |
| 28 | +X++: X is incremented by 1, X = 0 + 1 = 1. |
31 | 29 |
|
32 | 30 | **Example 2:** |
33 | 31 |
|
34 | 32 | **Input:** operations = ["++X","++X","X++"] |
35 | 33 |
|
36 | 34 | **Output:** 3 |
37 | 35 |
|
38 | | -**Explanation:** |
39 | | - |
40 | | -The operations are performed as follows: |
| 36 | +**Explanation:** The operations are performed as follows: |
41 | 37 |
|
42 | | -Initially, X = 0. |
| 38 | +Initially, X = 0. |
43 | 39 |
|
44 | | -++X: X is incremented by 1, X = 0 + 1 = 1. |
| 40 | +++X: X is incremented by 1, X = 0 + 1 = 1. |
45 | 41 |
|
46 | 42 | ++X: X is incremented by 1, X = 1 + 1 = 2. |
47 | 43 |
|
48 | | - X++: X is incremented by 1, X = 2 + 1 = 3. |
| 44 | +X++: X is incremented by 1, X = 2 + 1 = 3. |
49 | 45 |
|
50 | 46 | **Example 3:** |
51 | 47 |
|
52 | 48 | **Input:** operations = ["X++","++X","--X","X--"] |
53 | 49 |
|
54 | 50 | **Output:** 0 |
55 | 51 |
|
56 | | -**Explanation:** |
| 52 | +**Explanation:** The operations are performed as follows: |
57 | 53 |
|
58 | | -The operations are performed as follows: |
| 54 | +Initially, X = 0. |
59 | 55 |
|
60 | | -Initially, X = 0. |
61 | | - |
62 | | -X++: X is incremented by 1, X = 0 + 1 = 1. |
| 56 | +X++: X is incremented by 1, X = 0 + 1 = 1. |
63 | 57 |
|
64 | 58 | ++X: X is incremented by 1, X = 1 + 1 = 2. |
65 | 59 |
|
66 | | - --X: X is decremented by 1, X = 2 - 1 = 1. |
67 | | - |
68 | | -X--: X is decremented by 1, X = 1 - 1 = 0. |
| 60 | +--X: X is decremented by 1, X = 2 - 1 = 1. X--: X is decremented by 1, X = 1 - 1 = 0. |
69 | 61 |
|
70 | 62 | **Constraints:** |
71 | 63 |
|
72 | | -* `1 <= operations.length <= 100` |
73 | | -* `operations[i]` will be either `"++X"`, `"X++"`, `"--X"`, or `"X--"`. |
| 64 | +* `1 <= operations.length <= 100` |
| 65 | +* `operations[i]` will be either `"++X"`, `"X++"`, `"--X"`, or `"X--"`. |
0 commit comments