forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0682-baseball-game.java
More file actions
29 lines (26 loc) · 815 Bytes
/
0682-baseball-game.java
File metadata and controls
29 lines (26 loc) · 815 Bytes
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
class Solution {
public int calPoints(String[] operations) {
Stack<Integer> st = new Stack<>();
for(String op : operations) {
if(op.equals("+") && st.size() >= 2) {
int score1 = st.pop();
int score2 = st.peek();
int score3 = score1 + score2;
st.push(score1);
st.push(score3);
} else if(op.equals("D") && !st.isEmpty()) {
int score = st.peek();
st.push(score*2);
} else if(op.equals("C") && !st.isEmpty()) {
st.pop();
} else {
st.push(Integer.parseInt(op));
}
}
int sum = 0;
while(!st.isEmpty()) {
sum += st.pop();
}
return sum;
}
}