forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicCalculator.java
39 lines (38 loc) · 1.3 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
class Solution {
// TC : O(n)
public static int calculate(String s) {
int len = s.length();
int sign = 1;
int ans = 0;
int currNo = 0;
Stack<Integer> stack = new Stack<Integer>();
for (int i = 0; i < len; i++) {
if (Character.isDigit(s.charAt(i))) {
currNo = s.charAt(i) - '0';
while (i + 1 < len && Character.isDigit(s.charAt(i + 1))) {
currNo = currNo * 10 + s.charAt(i + 1) - '0';
i++;
}
currNo = currNo * sign;
ans += currNo;
currNo = 0;
sign = 1;
} else if (s.charAt(i) == '+')
sign = 1;
else if (s.charAt(i) == '-')
sign = -1; // -1 respresents negative sign
else if (s.charAt(i) == '(') {
stack.push(ans); // store the result calculated so far
stack.push(sign); // store the upcoming sign
ans = 0;
sign = 1;
} else if (s.charAt(i) == ')') {
int prevSign = stack.pop();
ans = prevSign* ans ;
int prevAns = stack.pop();
ans = ans + prevAns;
}
}
return ans;
}
}