772 Basic Calculator III
772. Basic Calculator III
1. Question
"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"=-122. Implementation
class Solution {
public int calculate(String s) {
if (s == null || s.length() == 0) {
return 0;
}
Stack<Integer> nums = new Stack();
Stack<Character> operators = new Stack();
int i = 0, num = 0;
int n = s.length();
while (i < n) {
char c = s.charAt(i);
if (Character.isDigit(c)) {
num = c - '0';
while (i + 1 < n && Character.isDigit(s.charAt(i + 1))) {
num = 10 * num + s.charAt(i + 1) - '0';
++i;
}
nums.push(num);
}
else if (isOperator(c)) {
// Handle negative number by pushing 0 to the stack
// so that we can get negative number in the calculation. For example 0 - 2 = -2
if (c == '-' && (i == 0 || s.charAt(i - 1) == '(')) {
nums.push(0);
}
while (!operators.isEmpty() && hasPrecedence(c, operators.peek())) {
nums.push(calculate(operators.pop(), nums.pop(), nums.pop()));
}
operators.push(c);
}
else if (c == '(') {
operators.push(c);
}
else if (c == ')') {
while (!operators.isEmpty() && operators.peek() != '(') {
nums.push(calculate(operators.pop(), nums.pop(), nums.pop()));
}
operators.pop();
}
++i;
}
while (!operators.isEmpty()) {
nums.push(calculate(operators.pop(), nums.pop(), nums.pop()));
}
return nums.isEmpty() ? 0 : nums.pop();
}
public boolean isOperator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/';
}
public int calculate(char operator, int num1, int num2) {
int res = 0;
switch(operator) {
case '+':
res = num1 + num2;
break;
case '-':
res = num2 - num1;
break;
case '*':
res = num1 * num2;
break;
case '/':
res = num2 / num1;
break;
}
return res;
}
public boolean hasPrecedence(char op1, char op2) {
if (op2 == '(' || op2 == ')') {
return false;
}
if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-')) {
return false;
}
return true;
}
}3. Time & Space Complexity
Last updated