-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0682.java
36 lines (30 loc) · 925 Bytes
/
0682.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
public int calPoints(String[] ops) {
Stack<Integer> st = new Stack<>();
int sum = 0;
for(String op : ops){
if(op.equals("C")){
if(st.isEmpty()) return 0;
st.pop();
}else if(op.equals("D")){
if(st.isEmpty()) return 0;
int m = st.peek();
int n = m * 2;
st.push(n);
}else if(op.equals("+")){
if(st.isEmpty()) return 0;
int a = st.pop();
int b = st.peek() + a;
st.push(a);
st.push(b);
}else{
st.push(Integer.valueOf(op));
}
}
if(st.isEmpty()) return 0;
while(!st.isEmpty()){
sum += st.pop();
}
return sum;
}
}