-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path682. Baseball Game
45 lines (37 loc) · 1.13 KB
/
682. Baseball Game
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
37
38
39
40
41
42
43
44
45
/*
682. Baseball Game
Runtime: 2 ms, faster than 87.95% of Java online submissions for Baseball Game.
Memory Usage: 38 MB, less than 93.69% of Java online submissions for Baseball Game.
*/
class Solution {
public int calPoints(String[] ops) {
Stack<Integer> s = new Stack<>();
for(int i=0; i<ops.length; i++){
switch(ops[i]){
case "D":
int l = s.peek();
int d = 2 * l;
s.push(d);
break;
case "C":
s.pop();
break;
case "+":
int n = s.pop();
int sum = s.peek() + n;
s.push(n);
s.push(sum);
break;
default:
int num = Integer.parseInt(ops[i]);
s.push(num);
break;
}
}
int total = 0;
while(!s.isEmpty()){
total += s.pop();
}
return total;
}
}