class Solution {
public int calPoints(String[] operations) {
Stack<Integer> s = new Stack();
for (String op : operations) {
if (op.equals("C")) {
s.pop();
} else if (op.equals("D")) {
s.push(2 * s.peek());
} else if (op.equals("+") && s.size() >= 2) {
int second = s.pop();
int first = s.pop();
int preTwoSum = first + second;
s.push(first);
s.push(second);
s.push(preTwoSum);
} else {
s.push(Integer.parseInt(op));
}
}
int res = 0;
while (!s.isEmpty()) {
res += s.pop();
}
return res;
}
}