-
Notifications
You must be signed in to change notification settings - Fork 0
/
Basic Calculator.java
54 lines (49 loc) · 1.78 KB
/
Basic Calculator.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
/*
Implement a basic calculator to evaluate a simple expression string. The expression string may contain open
( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces. You may
assume that the given expression is always valid.
Link: https://leetcode.com/problems/basic-calculator/
Example: None
Solution: Apply recursion to avoid stack.
Source: https://leetcode.com/discuss/83027/java-concise-fast-recursive-solution-with-comments-beats-61%25
*/
public class Solution {
public int calculate(String s) {
if (s.length() == 0) {
return 0;
}
s = "(" + s + ")";
int[] p = {0};
return eval(s, p);
}
// calculate value between parentheses
private int eval(String s, int[] p){
int val = 0;
int i = p[0];
int oper = 1; //1:+ -1:-
int num = 0;
while(i < s.length()){
char c = s.charAt(i);
switch(c){
case '+':
val = val + oper * num; num = 0; oper = 1; i++;
break;// end of number and set operator
case '-':
val = val + oper * num; num = 0; oper = -1; i++;
break;// end of number and set operator
case '(':
p[0] = i + 1; val = val + oper * eval(s, p); i = p[0];
break; // start a new eval
case ')':
p[0] = i + 1;
return val + oper * num; // end current eval and return. Note that we need to deal with the last num
case ' ':
i++;
continue;
default :
num = num * 10 + c - '0'; i++;
}
}
return val;
}
}