682. Baseball Game

class Solution {
    public int calPoints(String[] ops) {
        Deque<Integer> stack = new LinkedList<>();
        for(String option : ops){
            // switch statement with string
            switch(option){
                case "C":
                    stack.pop();
                    break;
                case "D":
                    stack.push(stack.peek()*2);
                    break;
                case "+":
                    int temp = stack.pop();
                    int top = stack.peek()+temp;
                    stack.push(temp);
                    stack.push(top);
                    break;
                default:
                    stack.push(Integer.parseInt(option));
            }
        }
        
        int result = 0;
        while(stack.size()>0){
            result+=stack.pop();
        }
        return result;
    }
}

Last updated