682. The baseball game

You are the recorder of a special baseball game. This match is made up of a number of rounds, and points scored in past rounds may affect points scored in future rounds.

At the start of the game, the record was blank. You get a list of strings to record operations ops, where OPS [I] is the ith operation you need to record. Ops follows the following rules:

The integer x – represents the number of points gained for the turn x “+” – represents the number of points gained for the turn is the sum of the previous two scores. The question data ensures that this operation is always preceded by two valid scores. “D” – Indicates that the new score scored in this turn is twice the previous score. The question data ensures that this operation is always preceded by a valid score when recording it. “C” – indicates that the previous score is invalid and removed from the record. The question data ensures that this operation is always preceded by a valid score when recording it. Return the sum of all the scores in the record.

Example 1:

Ops = ["5","2","C","D","+"] "5" - record plus 5, record is now [5] "2" - record plus 2, record is now [5, 2] "C" - invalidate the record of the previous score and remove it, record is now [5]. "D" - Record plus 2 * 5 = 10, Record is now [5, 10]. "+" - Record plus 5 + 10 = 15, record is now [5, 10, 15]. The sum of all points 5 + 10 + 15 = 30Copy the code

Example 2:

Input: ops = ["5"."- 2"."4"."C"."D"."9"."+"."+"] output:27Explanation:"5"- record and5, the record is now [5]
"- 2"- Record plus -2, the record is now [5, -2]
"4"- record and4, the record is now [5, -2.4]
"C"- Invalidate the previous score and remove it, the record is now [5, -2]
"D"- record and2 * -2 = -4, the record is now [5, -2, -4]
"9"- record and9, the record is now [5, -2, -4.9]
"+"- Record plus -4 + 9 = 5, the record is now [5, -2, -4.9.5]
"+"- record and9 + 5 = 14, the record is now [5, -2, -4.9.5.14] The sum of all points5 + -2 + -4 + 9 + 5 + 14 = 27
Copy the code

Example 3:

Input: ops = ["1"] Output: 1Copy the code

Tip:

1 <= ops. Length <= 1000 OPS [I] is “C”, “D”, “+”, or a string representing an integer. The integer range is [-3 * 104, 3 * 104] for the “+” operation, the problem data guarantees that there will always be a valid score before recording this operation for the “C” and “D” operations

Stack idea + once traversal

The result of the latest value is the result of the previous value

We use the stack directly to record each score

Iterate through OPS and get the score of each time according to the idea of the question, which is equivalent to adding, deleting, changing and checking the stack. Finally, all the scores need to be added to the stack again

To eliminate the need to iterate and add the scores, I simply update the current score on the first iteration

The current score of the corresponding operation when operating the stack each time, and the final score can be directly returned, eliminating the need for second traversal

var calPoints = function (ops) {
    var stack = []
    var score = 0
    while (ops.length) {
        var item = ops.shift()
        switch (item) {
            case '+':
                var num1 = stack[stack.length - 1] * 1
                var num2 = stack[stack.length - 2] * 1
                stack.push(num1 + num2)
                score += stack[stack.length - 1] * 1
                break;
            case 'D':
                stack.push(stack[stack.length - 1] * 2)
                score += stack[stack.length - 1] * 1
                break;
            case 'C':
                score -= stack[stack.length - 1] * 1
                stack.pop()
                break;
            default:
                stack.push(item)
                score += stack[stack.length - 1] * 1
                break; }}return score
};
Copy the code