We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 2ea2642 + a597ebf commit 76f44ecCopy full SHA for 76f44ec
java/0682-baseball-game.java
@@ -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