-
Notifications
You must be signed in to change notification settings - Fork 0
/
Basic Calculator II.java
58 lines (53 loc) · 2.13 KB
/
Basic Calculator II.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
57
58
/*
Implement a basic calculator to evaluate a simple expression string. The expression string contains
only non-negative integers, +, -, *, / operators and empty spaces . The integer division should
truncate toward zero. You may assume that the given expression is always valid.
Link: https://leetcode.com/problems/basic-calculator-ii/
Example: Some examples:
"3+2*2" = 7
" 3/2 " = 1
" 3+5 / 2 " = 5
Solution: None
Source: https://leetcode.com/discuss/83062/java-11ms-concise-and-fast-solution-with-comments-beats-02%25
*/
public class Solution {
public int calculate(String s) {
int val = 0;
int num = 0; //individual number in the string
int oper = 1; //1: + -1: -
int tmp = 1; //temp value for binary expressions that only have an operator of '*' or '/'
int tmpOper = 0; //temp operator for binary expressions 0: * 1: /
for (int i = 0; i < s.length(); i++){
char c = s.charAt(i);
switch(c){
case '+':
val = val + calc(tmp, tmpOper, num) * oper; num = 0; tmp = 1; tmpOper = 0; oper = 1;
break; //end of number
case '-':
val = val + calc(tmp, tmpOper, num) * oper; num = 0; tmp = 1; tmpOper = 0; oper = -1;
break; //end of number
case '*':
tmp = calc(tmp, tmpOper, num); num = 0; tmpOper = 0;
break;// end of number and keep calculating binary expression
case '/':
tmp = calc(tmp, tmpOper, num); num = 0; tmpOper = 1;
break;// end of number and keep calculating binary expression
case ' ':
continue;
default:
num = num * 10 + c - '0';
}
}
return val + calc(tmp, tmpOper, num) * oper; //deal with the last num
}
private int calc(int tmp, int tmpOper, int num){
switch(tmpOper){
case 0:
return tmp * num;
case 1:
return tmp / num;
default:
return 0;
}
}
}