682. Baseball Game 棒球游戏 按字母处理

[抄题]:

You're now a baseball game point recorder.

Given a list of strings, each string can be one of the 4 following types:

  1. Integer (one round's score): Directly represents the number of points you get in this round.
  2. "+" (one round's score): Represents that the points you get in this round are the sum of the last two valid round's points.
  3. "D" (one round's score): Represents that the points you get in this round are the doubled data of the last valid round's points.
  4. "C" (an operation, which isn't a round's score): Represents the last valid round's points you get were invalid and should be removed.

Each round's operation is permanent and could have an impact on the round before and the round after.

You need to return the sum of the points you could get in all the rounds.

Example 1:

Input: ["5","2","C","D","+"]
Output: 30
Explanation: 
Round 1: You could get 5 points. The sum is: 5.
Round 2: You could get 2 points. The sum is: 7.
Operation 1: The round 2's data was invalid. The sum is: 5.  
Round 3: You could get 10 points (the round 2's data has been removed). The sum is: 15.
Round 4: You could get 5 + 10 = 15 points. The sum is: 30.

Example 2:

Input: ["5","-2","4","C","D","9","+","+"]
Output: 27
Explanation: 
Round 1: You could get 5 points. The sum is: 5.
Round 2: You could get -2 points. The sum is: 3.
Round 3: You could get 4 points. The sum is: 7.
Operation 1: The round 3's data is invalid. The sum is: 3.  
Round 4: You could get -4 points (the round 3's data has been removed). The sum is: -1.
Round 5: You could get 9 points. The sum is: 8.
Round 6: You could get -4 + 9 = 5 points. The sum is 13.
Round 7: You could get 9 + 5 = 14 points. The sum is 27.

 [暴力解法]:

时间分析:

空间分析:

 [优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. string数组中的元素是string,不是char。用引号。但是字符串也可以用一个c字母来表示。
  2. 两倍的情况不能忘了push回来的值

[二刷]:

[三刷]:

[四刷]:

[五刷]:

  [五分钟肉眼debug的结果]:

[总结]:

stack起作用的时候,不外乎就是push pop方法

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

Integer.parseInt 用整数,把字符转化成整数了

[关键模板化代码]:

          int temp1 = stack.pop();
                int temp2 = stack.pop();
                int tempSum = temp1 + temp2;
                sum += tempSum;
                stack.push(temp2);
                stack.push(temp1);
                stack.push(tempSum);

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

 [代码风格] :

class Solution {
    public int calPoints(String[] ops) {
        //cc
        if (ops.length == 0) {
            return 0;
        }
        
        //ini: stack, sum
        Stack<Integer> stack = new Stack<>();
        int sum = 0;
        
        //4 states
        for (String c : ops) {
            if (c.equals("+")) {
                int temp1 = stack.pop();
                int temp2 = stack.pop();
                int tempSum = temp1 + temp2;
                sum += tempSum;
                stack.push(temp2);
                stack.push(temp1);
                stack.push(tempSum);
            }
            else if (c.equals("D")) {
                int temp = stack.pop();
                int temp_d = temp * 2;
                sum += temp_d;
                stack.push(temp);
                stack.push(temp_d);
            }
            else if (c.equals("C")) {
                int del = stack.pop();
                sum -= del;
            }
            else {
                int temp = Integer.parseInt(c);
                sum += temp;
                stack.push(temp);
            }
        }
        
        return sum;
    }
}
View Code
原文地址:https://www.cnblogs.com/immiao0319/p/8971164.html