| 1 | class Solution { |
| 2 | public int calPoints(String[] operations) { |
| 3 | Stack<Integer> st = new Stack<>(); |
| 4 | |
| 5 | for(String op : operations) { |
| 6 | if(op.equals("+") && st.size() >= 2) { |
| 7 | int score1 = st.pop(); |
| 8 | int score2 = st.peek(); |
| 9 | int score3 = score1 + score2; |
| 10 | st.push(score1); |
| 11 | st.push(score3); |
| 12 | } else if(op.equals("D") && !st.isEmpty()) { |
| 13 | int score = st.peek(); |
| 14 | st.push(score*2); |
| 15 | } else if(op.equals("C") && !st.isEmpty()) { |
| 16 | st.pop(); |
| 17 | } else { |
| 18 | st.push(Integer.parseInt(op)); |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | int sum = 0; |
| 23 | while(!st.isEmpty()) { |
| 24 | sum += st.pop(); |
| 25 | } |
| 26 | |
| 27 | return sum; |
| 28 | } |
| 29 | } |