java.lang.Object
g2001_2100.s2011_final_value_of_variable_after_performing_operations.Solution

public class Solution extends Object
2011 - Final Value of Variable After Performing Operations.<p>Easy</p> <p>There is a programming language with only <strong>four</strong> operations and <strong>one</strong> variable <code>X</code>:</p> <ul> <li><code>++X</code> and <code>X++</code> <strong>increments</strong> the value of the variable <code>X</code> by <code>1</code>.</li> <li><code>--X</code> and <code>X--</code> <strong>decrements</strong> the value of the variable <code>X</code> by <code>1</code>.</li> </ul> <p>Initially, the value of <code>X</code> is <code>0</code>.</p> <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> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> operations = [&ldquo;&ndash;X&rdquo;,&ldquo;X++&rdquo;,&ldquo;X++&rdquo;]</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong> The operations are performed as follows:</p> <p>Initially, X = 0.</p> <p>&ndash;X: X is decremented by 1, X = 0 - 1 = -1.</p> <p>X++: X is incremented by 1, X = -1 + 1 = 0.</p> <p>X++: X is incremented by 1, X = 0 + 1 = 1.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> operations = [&ldquo;++X&rdquo;,&ldquo;++X&rdquo;,&ldquo;X++&rdquo;]</p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong> The operations are performed as follows:</p> <p>Initially, X = 0.</p> <p>++X: X is incremented by 1, X = 0 + 1 = 1.</p> <p>++X: X is incremented by 1, X = 1 + 1 = 2.</p> <p>X++: X is incremented by 1, X = 2 + 1 = 3.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> operations = [&ldquo;X++&rdquo;,&ldquo;++X&rdquo;,&ldquo;&ndash;X&rdquo;,&ldquo;X&ndash;&rdquo;]</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> The operations are performed as follows:</p> <p>Initially, X = 0.</p> <p>X++: X is incremented by 1, X = 0 + 1 = 1.</p> <p>++X: X is incremented by 1, X = 1 + 1 = 2.</p> <p>&ndash;X: X is decremented by 1, X = 2 - 1 = 1.</p> <p>X&ndash;: X is decremented by 1, X = 1 - 1 = 0.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= operations.length <= 100</code></li> <li><code>operations[i]</code> will be either <code>&quot;++X&quot;</code>, <code>&quot;X++&quot;</code>, <code>&quot;--X&quot;</code>, or <code>&quot;X--&quot;</code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • finalValueAfterOperations

      public int finalValueAfterOperations(String[] operations)