-
Notifications
You must be signed in to change notification settings - Fork 1
/
BasicCalculatorII.java
53 lines (52 loc) · 1.55 KB
/
BasicCalculatorII.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
import java.util.ArrayDeque;
import java.util.Deque;
/**
* 227. Basic Calculator II
* 基本原则:用栈存储操作数,遇到 * 或者 / 时,直接运算,得结果入栈;遇到 + - 时,加上符号再入栈
* 最后再将栈清空即可。
* @author LBW
*/
public class BasicCalculatorII {
public int calculate(String s) {
char preSign = '+'; //
int len = s.length();
int i = 0;
Deque<Integer> stack = new ArrayDeque<>();
while (i < len) {
if (s.charAt(i) == ' ') {
i += 1;
continue;
}
if (s.charAt(i) >= '0' && s.charAt(i) <= '9') {
int num = 0;
while (i < len && s.charAt(i) >= '0' && s.charAt(i) <= '9') {
num = num * 10 + s.charAt(i) - '0';
i += 1;
}
if (preSign == '+') {
stack.push(num);
}
else if (preSign == '-') {
stack.push(-num);
}
else if (preSign == '*'){
int cur = stack.pop();
stack.push(cur * num);
}
else {
int cur = stack.pop();
stack.push(cur / num);
}
}
else {
preSign = s.charAt(i);
i += 1;
}
}
int res = 0;
while (!stack.isEmpty()) {
res += stack.pop();
}
return res;
}
}