-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSolution.java
More file actions
33 lines (28 loc) · 848 Bytes
/
Solution.java
File metadata and controls
33 lines (28 loc) · 848 Bytes
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
package _682;
class Solution {
public int calPoints(String[] ops) {
int[] scores = new int[1000];
int sum = 0;
int i = 0;
for (String op : ops) {
if (op.charAt(0) == 'C') {
sum -= scores[i--];
continue;
}
i++;
if (op.charAt(0) == '+') {
scores[i] = scores[i-1] + scores[i-2];
} else if (op.charAt(0) == 'D') {
scores[i] = scores[i - 1] * 2;
} else {
scores[i] = Integer.valueOf(op);
}
sum += scores[i];
}
return sum;
}
public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(solution.calPoints(new String[]{"5","-2","4","C","D","9","+","+"}));
}
}