Basic Calculator I
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.
Some examples:
"1 + 1" = 2 " 2-1 + 2 " = 3 "(1+(4+5+2)-3)+(6+8)" = 23
Note: Do not use the eval
built-in library function.
分析:这题因为不存在乘法和除法,所以,对于里面的减号,我们可以把它当成+(-num)来处理。所以,每次遇到一个数字的时候,我们需要知道这个数字的符号,每当我们把这个数字所有的digit都拿到以后,就可以得到这个数,然后把这个数加到之前的临时结果里。
对于比较特殊的处理是括号,但是这里有一个很巧的思路,我们可以把括号里的表达式call当前的方法来计算。
1 class Solution { 2 public int calculate(String s) { 3 int res = 0, num = 0, sign = 1, n = s.length(); 4 for (int i = 0; i < n; ++i) { 5 char c = s.charAt(i); 6 if (c >= ‘0‘ && c <= ‘9‘) { 7 num = 10 * num + (c - ‘0‘); 8 } else if (c == ‘(‘) { 9 int j = i, cnt = 0; 10 for (; i < n; ++i) { 11 char letter = s.charAt(i); 12 if (letter == ‘(‘) ++cnt; 13 if (letter == ‘)‘) --cnt; 14 if (cnt == 0) break; 15 } 16 num = calculate(s.substring(j + 1, i)); 17 } 18 if (c == ‘+‘ || c == ‘-‘ || i == n - 1) { 19 res += sign * num; 20 num = 0; 21 sign = (c == ‘+‘) ? 1 : -1; 22 } 23 } 24 return res; 25 } 26 }
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:
"3+2*2" = 7 " 3/2 " = 1 " 3+5 / 2 " = 5
Note: Do not use the eval
built-in library function.
分析:因为没有括号,所以我们可以把加或者减的部分当成一个数,比如 5-2,把它当成(5)+(-2)。同理,对于有乘或者除,或者既有乘又有除的话,也把它当成一个数,比如5-3*2/4=(5)-(3*2/4)。对于乘法和除法,我们总是从左算到右,所以我们可以把* or /之前的部分先存下来,当符号是 * or /的时候,再取出来就可以了。
1 class Solution { 2 public int calculate(String s) { 3 int res = 0, num = 0, n = s.length(); 4 char op = ‘+‘; 5 Stack<Integer> st = new Stack<>(); 6 for (int i = 0; i < n; ++i) { 7 char ch = s.charAt(i); 8 if (Character.isDigit(ch)) { 9 num = num * 10 + ch - ‘0‘; 10 } 11 if ((ch < ‘0‘ && ch != ‘ ‘) || i == n - 1) { 12 if (op == ‘+‘) st.push(num); 13 if (op == ‘-‘) st.push(-num); 14 if (op == ‘*‘ || op == ‘/‘) { 15 int tmp = (op == ‘*‘) ? st.pop() * num : st.pop() / num; 16 st.push(tmp); 17 } 18 op = ch; 19 num = 0; 20 } 21 } 22 while (!st.empty()) { 23 res += st.pop(); 24 } 25 return res; 26 } 27 }
Basic Calculator III
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-negativeintegers and empty spaces .
The expression string contains only non-negative integers, +
, -
, *
, /
operators , open (
and closing parentheses )
and empty spaces . The integer division should truncate toward zero.
You may assume that the given expression is always valid. All intermediate results will be in the range of [-2147483648, 2147483647]
.
Some examples:
"1 + 1" = 2 " 6-4 / 2 " = 4 "2*(5+5*2)/3+(6/2+8)" = 21 "(2+6* 3+5- (3*14/7+2)*5)+3"=-12
Note: Do not use the eval
built-in library function.
分析:只要把括号部分的处理加进来就可以了。
1 class Solution { 2 public static int calculate(String s) { 3 int res = 0, num = 0, n = s.length(); 4 char op = ‘+‘; 5 Stack<Integer> st = new Stack<>(); 6 for (int i = 0; i < n; ++i) { 7 char ch = s.charAt(i); 8 if (Character.isDigit(ch)) { 9 num = num * 10 + ch - ‘0‘; 10 } else if (ch == ‘(‘) { 11 int j = i, cnt = 0; 12 for (; i < n; ++i) { 13 char letter = s.charAt(i); 14 if (letter == ‘(‘) ++cnt; 15 if (letter == ‘)‘) --cnt; 16 if (cnt == 0) break; 17 } 18 num = calculate(s.substring(j + 1, i)); 19 } 20 if ((ch < ‘0‘ && ch != ‘ ‘) || i == n - 1) { 21 if (op == ‘+‘) st.push(num); 22 if (op == ‘-‘) st.push(-num); 23 if (op == ‘*‘ || op == ‘/‘) { 24 int tmp = (op == ‘*‘) ? st.pop() * num : st.pop() / num; 25 st.push(tmp); 26 } 27 op = ch; 28 num = 0; 29 } 30 } 31 while (!st.empty()) { 32 res += st.pop(); 33 } 34 return res; 35 } 36 }