224. Basic Calculator I && II && III
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 } else if (isOperator(ch)) { 11 addToStack(op, num, st); 12 op = ch; 13 num = 0; 14 } 15 } 16 // handle last case 17 addToStack(op, num, st); 18 while (!st.empty()) { 19 res += st.pop(); 20 } 21 return res; 22 } 23 24 private boolean isOperator(char ch) { 25 if (ch == '+' || ch == '-' || ch == '*' || ch == '/') { 26 return true; 27 } 28 return false; 29 } 30 31 private void addToStack(char op, int num, Stack<Integer> st) { 32 if (op == '+') st.push(num); 33 if (op == '-') st.push(-num); 34 if (op == '*' || op == '/') { 35 int tmp = (op == '*') ? st.pop() * num : st.pop() / num; 36 st.push(tmp); 37 } 38 } 39 }
class Solution { public int calculate(String s) { int num = 0; char operator = '+'; int last = 0, sum = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (Character.isDigit(c)) { num = num * 10 + (c - '0'); } else if (isOperator(c) || i == s.length() - 1) { if (operator == '+') { sum += last; last = num; } else if (operator == '-') { sum += last; last = -num; } else if (operator == '*') last *= num; else if (operator == '/') last /= num; num = 0; operator = c; } } return sum += last; } private boolean isOperator(char c) { return c == '+' || c == '-' || c == '*' || c == '/'; } }
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 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; 16 if (letter == ')') --cnt; 18 if (cnt == 0) break; 20 } 21 num = calculate(s.substring(j + 1, i)); 22 } else if (isOperator(ch)) { 23 addToStack(op, num, st); 24 op = ch; 25 num = 0; 26 } 27 } 28 // handle last case 29 addToStack(op, num, st); 30 while (!st.empty()) { 31 res += st.pop(); 32 } 33 return res; 34 } 35 36 private static boolean isOperator(char ch) { 37 if (ch == '+' || ch == '-' || ch == '*' || ch == '/') { 38 return true; 39 } 40 return false; 41 } 42 43 private static void addToStack(char op, int num, Stack<Integer> st) { 44 if (op == '+') st.push(num); 45 if (op == '-') st.push(-num); 46 if (op == '*' || op == '/') { 47 int tmp = (op == '*') ? st.pop() * num : st.pop() / num; 48 st.push(tmp); 49 } 50 } 51 }