share my java solution!


  • 0
    T
    class Solution {
        public int calPoints(String[] ops) {
            Deque<Integer> deque = new ArrayDeque<>();
            Set<String> set = new HashSet<>();
            set.add("+");
            set.add("D");
            set.add("C");
            
            for(int i=0;i<ops.length;i++){
                if(!set.contains(ops[i])){
                    deque.offer(Integer.parseInt(ops[i]));
                }else{
                    if(ops[i].equals("+")){
                        int last_one = deque.pollLast();
                        int last_two = deque.pollLast();
                        deque.offer(last_two);
                        deque.offer(last_one);
                        deque.offer(last_one+last_two);
                    }else if(ops[i].equals("D")){
                        int last_one = deque.peekLast();
                        deque.offer(last_one*2);
                    }else if(ops[i].equals("C")){
                        deque.pollLast();
                    }
                }
            }
            int sum = 0;
            while(!deque.isEmpty()){
                sum+=deque.pollFirst();
            }
            
            return sum;
            
        }
    }
    
    

Log in to reply
 

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