-
-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy path682.java
77 lines (69 loc) · 2.24 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
__________________________________________________________________________________________________
sample 1 ms submission
class Solution {
public int calPoints(String[] ops) {
String[] arrayWithoutC = new String[ops.length];
int altI = 0;
for (int i = 0; i < ops.length; ++i) {
switch (ops[i]) {
case "C":
if (altI > 0) {
--altI;
arrayWithoutC[altI] = null;
}
break;
default:
arrayWithoutC[altI] = ops[i];
++altI;
}
}
int sum = 0;
int prev = 0;
int prev2 = 0;
int newPrev = 0;
for (String op : arrayWithoutC) {
if (op == null) {
continue;
}
switch (op) {
case "+":
newPrev = prev + prev2;
break;
case "D":
newPrev = prev * 2;
break;
default:
newPrev = Integer.parseInt(op);
}
sum += newPrev;
prev2 = prev;
prev = newPrev;
}
return sum;
}
}
__________________________________________________________________________________________________
sample 34920 kb submission
class Solution {
public int calPoints(String[] ops) {
Stack<Integer> stack = new Stack();
for(String op : ops) {
if (op.equals("+")) {
int top = stack.pop();
int newtop = top + stack.peek();
stack.push(top);
stack.push(newtop);
} else if (op.equals("C")) {
stack.pop();
} else if (op.equals("D")) {
stack.push(2 * stack.peek());
} else {
stack.push(Integer.valueOf(op));
}
}
int ans = stack.stream().mapToInt(Integer::intValue).sum();
// for(int score : stack) ans += score;
return ans;
}
}
__________________________________________________________________________________________________