LeetCode(Stack)682. Baseball Game

1.问题

You are keeping the scores for a baseball game with strange rules. At the beginning of the game, you start with an empty record.

You are given a list of strings operations, where operations[i] is the ith operation you must apply to the record and is one of the following:

An integer x.
Record a new score of x.
‘+’.
Record a new score that is the sum of the previous two scores.
‘D’.
Record a new score that is the double of the previous score.
‘C’.
Invalidate the previous score, removing it from the record.
Return the sum of all the scores on the record after applying all the operations.

The test cases are generated such that the answer and all intermediate calculations fit in a 32-bit integer and that all operations are valid.

Example 1:

Input: ops = [“5”,“2”,“C”,“D”,“+”]
Output: 30
Explanation:
“5” - Add 5 to the record, record is now [5].
“2” - Add 2 to the record, record is now [5, 2].
“C” - Invalidate and remove the previous score, record is now [5].
“D” - Add 2 * 5 = 10 to the record, record is now [5, 10].
“+” - Add 5 + 10 = 15 to the record, record is now [5, 10, 15].
The total sum is 5 + 10 + 15 = 30.

Example 2:

Input: ops = [“5”,“-2”,“4”,“C”,“D”,“9”,“+”,“+”]
Output: 27
Explanation:
“5” - Add 5 to the record, record is now [5].
“-2” - Add -2 to the record, record is now [5, -2].
“4” - Add 4 to the record, record is now [5, -2, 4].
“C” - Invalidate and remove the previous score, record is now [5, -2].
“D” - Add 2 * -2 = -4 to the record, record is now [5, -2, -4].
“9” - Add 9 to the record, record is now [5, -2, -4, 9].
“+” - Add -4 + 9 = 5 to the record, record is now [5, -2, -4, 9, 5].
“+” - Add 9 + 5 = 14 to the record, record is now [5, -2, -4, 9, 5, 14].
The total sum is 5 + -2 + -4 + 9 + 5 + 14 = 27.

Example 3:

Input: ops = [“1”,“C”]
Output: 0
Explanation:
“1” - Add 1 to the record, record is now [1].
“C” - Invalidate and remove the previous score, record is now [].
Since the record is empty, the total sum is 0.

Constraints:

  • 1 <= operations.length <= 1000
  • operations[i] is “C”, “D”, “+”, or a string representing an integer in the - range [-3 * 104, 3 * 104].
  • For operation “+”, there will always be at least two previous scores on the record.
  • For operations “C” and “D”, there will always be at least one previous score on the record.

2. 解题思路

方法1:

1.如果当前操作是数字,则将其转化为整型后入栈。
2.如果当前操作是加分,则弹出栈顶元素和下面的元素,将它们相加后将结果入栈。
3.如果当前操作是翻倍,则将栈顶元素翻倍后入栈。
4.如果当前操作是撤销,则弹出栈顶元素。
5.最后将栈中所有元素依次弹出并累加起来,即为最终得分。

方法2:

1.如果当前操作是撤销,则将最后一个得分从链表中删除,并从 sum 中减去该得分;
2.如果当前操作是翻倍,则将上一个得分翻倍后添加到链表末尾,并加入 sum 中;
3.如果当前操作是加分,则将上一个得分和上上个得分相加后添加到链表末尾,并加入 sum 中;
4.如果当前操作是数字,则将该数字转化为整型后添加到链表末尾,并加入 sum 中。

3. 代码

代码1:

class Solution {
    public int calPoints(String[] operations) {
        int result = 0; // 初始化分数
        Stack<Integer> stack = new Stack<Integer>(); // 1.创建一个栈来保存分数
        for (int i = 0; i < operations.length; i++) {
            if (operations[i].equals("+")) { // 2.如果当前操作是加分
                int last = stack.pop(); // 取出栈顶元素
                int secondLast = stack.peek(); // 取出栈顶下面的元素
                stack.push(last); // 将最后一个元素重新入栈
                stack.push(last + secondLast); // 将加分后的分数入栈
            } else if (operations[i].equals("D")) { // 3.如果当前操作是翻倍
                stack.push(stack.peek() * 2); // 4.将当前分数翻倍后入栈
            } else if (operations[i].equals("C")) { // 5.如果当前操作是无效操作
                stack.pop(); // 弹出栈顶元素
            } else { // 6.如果当前操作是有效数字
                stack.push(Integer.valueOf(operations[i])); // 将数字转化为整型后入栈
            }
        }
        while (!stack.isEmpty()) { // 7.计算最终得分
            result += stack.pop(); // 将栈中的每个分数依次弹出并累加到结果中
        }
        return result; // 返回最终得分
    }
}

代码2:

class Solution {
    public int calPoints(String[] operations) {
        int sum = 0; // 初始化总得分
        LinkedList<Integer> list = new LinkedList<>(); // 创建一个链表来保存每个得分
        for (String op : operations) {
            if (op.equals("C")) { // 如果当前操作是撤销
                sum -= list.removeLast(); // 将最后一个得分从总得分中减去,并从链表中删除
            }
            else if (op.equals("D")) { // 如果当前操作是翻倍
                list.add(list.peekLast() * 2); // 将上一个得分翻倍后添加到链表末尾
                sum += list.peekLast(); // 将该得分加入总得分中
            }
            else if (op.equals("+")) { // 如果当前操作是加分
                list.add(list.peekLast() + list.get(list.size() - 2)); // 将上一个得分和上上个得分相加后添加到链表末尾
                sum += list.peekLast(); // 将该得分加入总得分中
            }
            else { // 如果当前操作是数字
                list.add(Integer.parseInt(op)); // 将数字转化为整型后添加到链表末尾
                sum += list.peekLast(); // 将该得分加入总得分中
            }
        }
        return sum; // 返回总得分
    }
}

解题思路基本相同,使用的数据结构不同,使用ArrayList

class Solution {
    public int calPoints(String[] operations) {
       int ans = 0;
       ArrayList<Integer> list = new ArrayList<>();
      for(int i=0;i<operations.length;i++){
           if(operations[i].equals("+")){
               int sum = list.get(list.size()-1)+list.get(list.size()-2);
               list.add(sum);
           }
           else if(operations[i].equals("D")){
               int prod = 2*list.get(list.size()-1);
               list.add(prod);
           }
           else if(operations[i].equals("C")){
               list.remove(list.size()-1);
           }
           else{
               int t = Integer.parseInt(operations[i]);
               list.add(t);
           }
       }
       for(int i=0;i<list.size();i++){
           ans +=list.get(i);
       }
       return ans;
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值