forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
40 lines (39 loc) · 1.21 KB
/
Solution.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
class Solution {
public int calculate(String s) {
char[] cs = s.toCharArray();
Deque<Character> op = new ArrayDeque<>();
Deque<Integer> num = new ArrayDeque<>();
for (int i = 0; i < cs.length; ++i) {
if (cs[i] == '(' || cs[i] == '+' || cs[i] == '-') {
op.push(cs[i]);
} else if (cs[i] == ')') {
op.pop();
if (!op.isEmpty() && op.peek() != '(') {
calc(op, num);
}
} else if (Character.isDigit(cs[i])) {
int j = i;
int k = 0;
while (j < cs.length && Character.isDigit(cs[j])) {
k = k * 10 + cs[j] - '0';
++j;
}
num.push(k);
i = j - 1;
if (!op.isEmpty() && op.peek() != '(') {
calc(op, num);
}
}
}
return num.peek();
}
private void calc(Deque<Character> op, Deque<Integer> num) {
int y = num.pop();
int x = num.pop();
if (op.pop() == '+') {
num.push(x + y);
} else {
num.push(x - y);
}
}
}