-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasic Calculator III.java
56 lines (51 loc) · 1.89 KB
/
Basic Calculator III.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
class Solution {
public int calculate(String s) {
// stack to store previous values and operands
// 2 lvls, lvl1 -> (+, -), lvl2 -> (*, /), marked by op = {1, -1}
Stack<Integer> stack = new Stack<>();
int prev1 = 0, prev2 = 1, op1 = 1, op2 = 1;
// digit, +/-, *//, ()
for(int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if(Character.isDigit(c)) {
int num = 0;
while(i < s.length() && Character.isDigit(s.charAt(i))) {
num = 10*num + s.charAt(i++) - '0';
}
// to offset the i so that nothing is omitted
i--;
// store in lvl2
prev2 = (op2==1? prev2*num: prev2/num);
}
else if(c == '+' || c == '-') {
// save the current lvl2
prev1 += op1*prev2;
op1 = ((c=='+')? 1: -1);
// reset lvl2
prev2 = 1;
op2 = 1;
}
else if(c == '*' || c == '/') {
op2 = ((c=='*')? 1: -1);
}
else if(c == '(') {
stack.push(prev1);
stack.push(prev2);
stack.push(op1);
stack.push(op2);
prev1 = 0; op1 = 1;
prev2 = 1; op2 = 1;
}
else if(c == ')') {
// save the current value inside the parentheses
int cur = prev1 + op1*prev2;
op2 = stack.pop();
op1 = stack.pop();
prev2 = stack.pop();
prev1 = stack.pop();
prev2 = ((op2==1)? prev2*cur: prev2/cur);
}
}
return prev1 + op1*prev2;
}
}