-
Notifications
You must be signed in to change notification settings - Fork 2
/
BasicCalculator.java
50 lines (47 loc) · 1.66 KB
/
BasicCalculator.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
package src.java.hard.string;
import java.util.LinkedList;
public class BasicCalculator {
public int calculate(String s) {
if (s==null || s.isEmpty()) return 0;
LinkedList<Integer> nums = new LinkedList<>();
LinkedList<Integer> parentheses = new LinkedList<>();
int i = 0, sign = 1;
while (i<s.length()){
char c = s.charAt(i);
if (Character.isDigit(c)) {
int t = i;
while (i<s.length() && Character.isDigit(s.charAt(i)))
i++;
nums.push(Integer.parseInt(s.substring(t, i)) * sign);
sign = 1;
continue;
} else if (c == '(') {
if (sign > 0)
parentheses.push(i);
else {
parentheses.push(-i);
sign = 1;
}
} else if (c == ')') {
int num = 0;
int index = parentheses.peek();
while (!parentheses.isEmpty() && index == parentheses.peek()) {
num += nums.pop();
parentheses.pop();
}
if (index >= 0) nums.push(num);
else nums.push(-num);
} else if (c == '+' && !parentheses.isEmpty()) {
parentheses.push(parentheses.peek());
} else if (c == '-') {
sign = -1;
if (!parentheses.isEmpty())
parentheses.push(parentheses.peek());
}
i++;
}
int ret = 0;
while (!nums.isEmpty()) ret += nums.pop();
return ret;
}
}