LeetCode: Basic Calculator II

LeetCode: Basic Calculator II

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.

Some examples:

1
2
3
"3+2*2" = 7
" 3/2 " = 1
" 3+5 / 2 " = 5

Note: Do not use the eval built-in library function.

https://leetcode.com/problems/basic-calculator-ii/

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
public class BasicCalculatorII {
public int calculate(String s) {
if (s == null || s.trim().length() == 0) {
return 0;
}

s = s.replaceAll(" ", "");

List<Integer> list = new ArrayList<Integer>();

for (int i = 0; i < s.length(); ) {
char c = s.charAt(i);
int op = isOperator(c);
int end = i + 1;
if (op == 2) {
// for * and /, just calculate the result and store it in the list
int a = list.get(list.size() - 1);
list.remove(list.size() - 1);
end = readNumber(s, i + 1);
list.add(getResult(a, Integer.valueOf(s.substring(i + 1, end)), c));
} else if (op == 1 && c == '-') {
// if minus a number, just store thr negative format number in the list
end = readNumber(s, i + 1);
list.add(Integer.valueOf(s.substring(i, end)));
}

if (isNumber(c)) {
end = readNumber(s, i);
list.add(Integer.valueOf(s.substring(i, end)));
}

i = end;
}

// sum up all integers
int result = 0;
for (int a : list) {
result += a;
}

return result;
}

/**
* Read number, and find the last index for current number
*
* @param s given string
* @param start start index to search number
* @return last index of current number
*/
private int readNumber(String s, int start) {
while (start < s.length()) {
if (!isNumber(s.charAt(start))) {
break;
}
start++;
}
return start;
}

/**
* check a char is number or not
*
* @param c char to check
* @return true for number otherwise false
*/
private boolean isNumber(char c) {
return c >= '0' && c <= '9';
}

/**
* Check whether it is a operator
*
* @param c char
* @return 2 for (*, /), 1 for (+,-), otherwise 0
*/
private int isOperator(char c) {
if (c == '*' || c == '/') {
return 2;
} else if (c == '+' || c == '-') {
return 1;
} else {
return 0;
}
}

/**
* Calculate the result
*
* @param x first parameter
* @param y second parameter
* @param c operator
* @return result
*/
private int getResult(int x, int y, char c) {
switch (c) {
case '+':
return x + y;
case '-':
return x - y;
case '*':
return x * y;
case '/':
return x / y;
default:
return 0;
}
}
}