Loading...
You maintain a list of integers that starts empty. You are given an array of strings operations, where operations[i] is the i-th operation to apply to the list. Each operation is one of the following:
x (as a string): append x to the list."+": append the sum of the last two values in the list."D": append double the last value in the list."C": remove the last value from the list.Apply all operations in order and return the sum of every value remaining in the list.
All operations are guaranteed to be valid: "+" only appears when the list has at least two values, and "C" and "D" only appear when it has at least one. The final answer and every value ever appended fit in a 32-bit integer.
Input: operations = ["5","2","C","D","+"]
Output: 30
Explanation:
"5" - append 5; the list is [5].
"2" - append 2; the list is [5, 2].
"C" - remove the last value; the list is [5].
"D" - append 2 * 5 = 10; the list is [5, 10].
"+" - append 5 + 10 = 15; the list is [5, 10, 15].
The total is 5 + 10 + 15 = 30.
Input: operations = ["5","-2","4","C","D","9","+","+"]
Output: 27
Explanation:
"5" - append 5; the list is [5].
"-2" - append -2; the list is [5, -2].
"4" - append 4; the list is [5, -2, 4].
"C" - remove the last value; the list is [5, -2].
"D" - append 2 * -2 = -4; the list is [5, -2, -4].
"9" - append 9; the list is [5, -2, -4, 9].
"+" - append -4 + 9 = 5; the list is [5, -2, -4, 9, 5].
"+" - append 9 + 5 = 14; the list is [5, -2, -4, 9, 5, 14].
The total is 5 + -2 + -4 + 9 + 5 + 14 = 27.
Input: operations = ["1","C"]
Output: 0
Explanation:
"1" - append 1; the list is [1].
"C" - remove the last value; the list is [].
The list is empty, so the total is 0.
operations.length ≤1000operations[i] is "C", "D", "+", or a string representing an integer in the range [−3⋅104,3⋅104]."+", there are always at least two previous values in the list."C" and "D", there is always at least one previous value in the list.Click "Run" to test with sample cases or "Submit" to run all tests.