美文网首页
Leetcode 682

Leetcode 682

作者: 成江 | 来源:发表于2018-05-30 21:22 被阅读80次
// Line 25: error: cannot find symbol: method parseInt()
/* Exception in thread "main" java.util.EmptyStackException
    at java.util.Stack.peek(Stack.java:59)
    at Solution.calPoints(Solution.java:19)
    at __DriverSolution__.__helper__(__Driver__.java:8)
    at __Driver__.main(__Driver__.java:52)
*/

class Solution {
    public int calPoints(String[] ops) {
        if (ops == null) return 0;
        Stack<Integer> stack = new Stack<Integer>();
        int sum = 0;
        for (int i = 0; i < ops.length; i++) {
            if (ops[i].equals("C")) {
                if(stack.size() != 0) {
                    int lastRound = stack.pop();
                    sum -= lastRound;
                }
            } else if (ops[i].equals("D")) {
                // need to check the size of stack?
                int lastRound = stack.peek();
                sum += 2*lastRound;
            } else if (ops[i].equals("+")) {
                if (stack.size() != 0) {
                    int last1 = stack.pop();
                    int last2 = stack.peek();
                    stack.push(last1);
                    sum += (last1 + last2);
                }
                
            } else {
                int score = ops[i].parseInt();
                stack.push(score);
                sum += score;
            }
        }
        return sum;
    }
}

// Wrong answer
// didn't understand the question clearly

class Solution {
    public int calPoints(String[] ops) {
        if (ops == null) return 0;
        Stack<Integer> stack = new Stack<Integer>();
        int sum = 0;
        for (int i = 0; i < ops.length; i++) {
            if (ops[i].equals("C")) {
                if(stack.size() != 0) {
                    int lastRound = stack.pop();
                    sum -= lastRound;
                }
            } else if (ops[i].equals("D")) {
                // need to check the size of stack?
                if (stack.size() != 0) {
                    int lastRound = stack.peek();
                    stack.push(2*lastRound);
                    sum += 2*lastRound;
                }
                
            } else if (ops[i].equals("+")) {
                if (stack.size() > 1) {
                    int last1 = stack.pop();
                    int last2 = stack.peek();
                    stack.push(last1);
                    stack.push(last1 + last2);
                    sum += (last1 + last2);
                }
                
            } else {
                int score = Integer.parseInt(ops[i]);
                stack.push(score);
                sum += score;
            }
        }
        return sum;
    }
}

相关文章

  • Leetcode 682

  • LeetCode 682. Baseball Game + 49

    Algorithm 第六周 LeetCode 682. Baseball Game 题目链接 解题语言 C 你现在...

  • 682. 棒球比赛、面试题 03.04. 化栈为队

    今天是栈操作。 682. 棒球比赛[https://leetcode-cn.com/problems/baseba...

  • Leetcode 682. Baseball Game

    文章作者:Tyan博客:noahsnail.com | CSDN | 简书 1. Description 2. S...

  • leetcode 题号#682 棒球比赛

    查看题目详情可点击此处。 题目 你现在是棒球比赛记录员。给定一个字符串列表,每个字符串可以是以下四种类型之一: 整...

  • leetcode刷题-栈

    leetcode上关于栈的题目大家可以先做20,155,232,844,224,682,496. 20给定一个只包...

  • 682

    第一章 公主殉国 圣元二十年春 西域犯陈。 胡人部落首领李长卿举兵,大破陈国京都,陈国国君及皇后双双自缢身亡。 山...

  • Leetcode PHP题解--D37 682. Basebal

    682. Baseball Game 题目链接 682. Baseball Game 题目分析 给定一个字符串数组...

  • 亲子 (682)

    2019.2.11 星期一 晴 今天俩娃都叫妹妹妹夫接去了,收拾收拾准备明天开业了。 三(2)邵艺馨妈妈

  • 亲子 (682)

    2019.2.11 星期一 晴 今天俩娃都叫妹妹妹夫接去了,收拾收拾准备明天开业了。 三(2)邵艺馨妈妈

网友评论

      本文标题:Leetcode 682

      本文链接:https://www.haomeiwen.com/subject/vowxsftx.html