-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseball.java
More file actions
54 lines (46 loc) · 1.35 KB
/
Baseball.java
File metadata and controls
54 lines (46 loc) · 1.35 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
47
48
49
50
51
52
53
54
package Stack;
import java.util.Stack;
/**
* Author - archit.s
* Date - 22/08/18
* Time - 12:13 PM
*/
public class Baseball {
public int calPoints(String[] ops) {
int length = ops.length;
int sum = 0;
Stack<Integer> s = new Stack<>();
int num, num1, num2;
for (String op : ops) {
switch (op) {
case "C":
num = s.pop();
sum -= num;
break;
case "D":
num = s.pop();
sum += 2 * num;
s.push(num);
s.push(2 * num);
break;
case "+":
num2 = s.pop();
num1 = s.pop();
sum += num1 + num2;
s.push(num1);
s.push(num2);
s.push(num1 + num2);
break;
default:
num = Integer.valueOf(op);
sum += num;
s.push(num);
}
}
return sum;
}
public static void main(String[] args) {
String[] s = new String[]{"57","D","-3","-58","D","77","+","C","+","+","38","78","-6","24","-46","+","31","20","D","-81"};
System.out.println(new Baseball().calPoints(s));
}
}