|
| 1 | +<h2><a href="https://leetcode.com/problems/final-value-of-variable-after-performing-operations">2137. Final Value of Variable After Performing Operations</a></h2><h3>Easy</h3><hr><p>There is a programming language with only <strong>four</strong> operations and <strong>one</strong> variable <code>X</code>:</p> |
| 2 | + |
| 3 | +<ul> |
| 4 | + <li><code>++X</code> and <code>X++</code> <strong>increments</strong> the value of the variable <code>X</code> by <code>1</code>.</li> |
| 5 | + <li><code>--X</code> and <code>X--</code> <strong>decrements</strong> the value of the variable <code>X</code> by <code>1</code>.</li> |
| 6 | +</ul> |
| 7 | + |
| 8 | +<p>Initially, the value of <code>X</code> is <code>0</code>.</p> |
| 9 | + |
| 10 | +<p>Given an array of strings <code>operations</code> containing a list of operations, return <em>the <strong>final </strong>value of </em><code>X</code> <em>after performing all the operations</em>.</p> |
| 11 | + |
| 12 | +<p> </p> |
| 13 | +<p><strong class="example">Example 1:</strong></p> |
| 14 | + |
| 15 | +<pre> |
| 16 | +<strong>Input:</strong> operations = ["--X","X++","X++"] |
| 17 | +<strong>Output:</strong> 1 |
| 18 | +<strong>Explanation:</strong> The operations are performed as follows: |
| 19 | +Initially, X = 0. |
| 20 | +--X: X is decremented by 1, X = 0 - 1 = -1. |
| 21 | +X++: X is incremented by 1, X = -1 + 1 = 0. |
| 22 | +X++: X is incremented by 1, X = 0 + 1 = 1. |
| 23 | +</pre> |
| 24 | + |
| 25 | +<p><strong class="example">Example 2:</strong></p> |
| 26 | + |
| 27 | +<pre> |
| 28 | +<strong>Input:</strong> operations = ["++X","++X","X++"] |
| 29 | +<strong>Output:</strong> 3 |
| 30 | +<strong>Explanation: </strong>The operations are performed as follows: |
| 31 | +Initially, X = 0. |
| 32 | +++X: X is incremented by 1, X = 0 + 1 = 1. |
| 33 | +++X: X is incremented by 1, X = 1 + 1 = 2. |
| 34 | +X++: X is incremented by 1, X = 2 + 1 = 3. |
| 35 | +</pre> |
| 36 | + |
| 37 | +<p><strong class="example">Example 3:</strong></p> |
| 38 | + |
| 39 | +<pre> |
| 40 | +<strong>Input:</strong> operations = ["X++","++X","--X","X--"] |
| 41 | +<strong>Output:</strong> 0 |
| 42 | +<strong>Explanation:</strong> The operations are performed as follows: |
| 43 | +Initially, X = 0. |
| 44 | +X++: X is incremented by 1, X = 0 + 1 = 1. |
| 45 | +++X: X is incremented by 1, X = 1 + 1 = 2. |
| 46 | +--X: X is decremented by 1, X = 2 - 1 = 1. |
| 47 | +X--: X is decremented by 1, X = 1 - 1 = 0. |
| 48 | +</pre> |
| 49 | + |
| 50 | +<p> </p> |
| 51 | +<p><strong>Constraints:</strong></p> |
| 52 | + |
| 53 | +<ul> |
| 54 | + <li><code>1 <= operations.length <= 100</code></li> |
| 55 | + <li><code>operations[i]</code> will be either <code>"++X"</code>, <code>"X++"</code>, <code>"--X"</code>, or <code>"X--"</code>.</li> |
| 56 | +</ul> |
0 commit comments