-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBasicCalculator.java
45 lines (40 loc) · 1.28 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
/*https://leetcode.com/problems/basic-calculator/*/
class Solution {
public int calculate(String s) {
Stack<Integer> st = new Stack<>();
int sum = 0, sign = 1;
for(int i = 0; i < s.length(); ++i)
{
char ch = s.charAt(i);
//if the character is a digit, trace the entire number and store it along with its sign
if (Character.isDigit(ch))
{
int val = 0;
while(i < s.length() && Character.isDigit(s.charAt(i)))
val = val*10+(s.charAt(i++)-'0');
--i;
val = val*sign;
sum += val;
sign = 1;
}
//for open brackets, push the result and the sign to stack and reset them
else if (ch == '(')
{
st.push(sum);
st.push(sign);
sum = 0;
sign = 1;
}
//for closing brackets, pop the result and the sign from the stack
else if (ch == ')')
{
sum *= st.pop();
sum += st.pop();
}
//for unary -, change the sign
else if (ch == '-')
sign *= -1;
}
return sum;
}
}