Skip to content

Commit 76f44ec

Browse files
authored
Merge pull request #1858 from gnohgnij/682-baseball-game
Create: 0682-baseball-game.java
2 parents 2ea2642 + a597ebf commit 76f44ec

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

java/0682-baseball-game.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
}

0 commit comments

Comments
 (0)