-
Notifications
You must be signed in to change notification settings - Fork 0
/
Baseball_Game_682.java
45 lines (39 loc) · 1011 Bytes
/
Baseball_Game_682.java
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
package leetcode;
import java.util.ArrayList;
class Solution682 {
public int calPoints(String[] operations) {
ArrayList<Integer> score = new ArrayList<>();
for (int i = 0; i < operations.length; i++) {
switch (operations[i]) {
case "C":
score.remove(score.size() - 1);
break;
case "D":
score.add(score.get(score.size() - 1) * 2);
break;
case "+":
score.add(score.get(score.size() - 1) + score.get(score.size() - 2));
break;
default:
score.add(Integer.valueOf(operations[i]));
}
}
return sum(score);
}
public static int sum(ArrayList<Integer> scores) {
int sum = 0;
for (int s : scores) {
sum += s;
}
return sum;
}
}
public class Baseball_Game_682 {
public static void main(String[] args) {
Solution682 ns = new Solution682();
// String operations[] = { "5", "2", "C", "D", "+" };
// String operations[]= {"5","-2","4","C","D","9","+","+"};
String operations[] = { "1", "C" };
System.out.println(ns.calPoints(operations));
}
}