-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode682_baseballGame.java
More file actions
46 lines (36 loc) · 1.13 KB
/
Copy pathleetcode682_baseballGame.java
File metadata and controls
46 lines (36 loc) · 1.13 KB
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
46
import java.util.Stack;
public class leetcode682_baseballGame {
public int calPoints(String[] ops) {
Stack<Integer> stack = new Stack<>();
int tmp1,tmp2,sum=0;
for(int i=0;i<ops.length;i++){
if(ops[i].equals("+")){
tmp1 = stack.pop();
tmp2 = stack.pop();
stack.add(tmp2);
stack.add(tmp1);
stack.add(tmp1+tmp2);
}
else if(ops[i].equals("C")){
stack.pop();
}
else if(ops[i].equals("D")){
tmp1 = stack.peek();
stack.add(tmp1*2);
}
else{
stack.add(Integer.parseInt(ops[i]));
}
}
while(!stack.empty()){
sum = sum + stack.pop();
}
//System.out.println(sum);
return sum;
}
public static void main(String[] args) {
String[] str = {"5","2","C","D","+"};
leetcode682_baseballGame game = new leetcode682_baseballGame();
System.out.println(game.calPoints(str));
}
}