Simple Java solution


  • 0
    public int calPoints(String[] ops) {
        if (ops == null || ops.length == 0){
            return 0;
        }
        ArrayList<Integer> list = new ArrayList();
        int cur = 0, sum = 0;
        int len = -1;
        for (String s : ops){
            switch (s){
                case "+" :
                    cur = list.get(len) + list.get(len - 1);
                    list.add(cur);
                    ++ len;
                    break;
                case "C":
                    list.remove(len);
                    -- len;
                    break;
                case "D":
                    cur = 2 * list.get(len);
                    list.add(cur);
                    ++ len;
                    break;
                default:
                    cur = Integer.parseInt(s);
                    list.add(cur);
                    ++ len;
            }
        }
        
        for (int n : list){
            sum += n;
        }
        return sum;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.