Basic Calculator
Implement a basic calculator to evaluate a simple expression string.
Problem 1:
The string only contains '+' '-' '(' ')' 0-9 and spaces. You may assume the given expression is valid.
e.g: "(1+(4+5+2)-3)+(6+8)" return 23;
Solution:
1. we scan the whole string and use a stack to store the temporary result of the expression. We use a variable n to store temporary number and another variable s to store temporary sign of number.
2. whenever we encounter a digit, we update n to 10*n+(c-'0')
3. whenever we encounter '+' or '-', we update the result to result+sign*number. And set sign to new sign, set number to 0.
4. whenever we encounter '(', we push current result to stack, and push sign to the stack (this sign is the sign of the result between parentheses). Initialize result to 0 and sign to 1.
5. whenever we encounter ')', we update the result to result+sign*number. Then update result as result*stack.pop()+stack.pop(). And set number to 0.
6. After scanning the string, if number is not 0, we add number*sign to result.
7. return the result.
Code:
public class Solution { public int calculate(String s) { Stack<Integer> st = new Stack<>(); int result = 0; int sign = 1; int number = 0; char[] arr = s.toCharArray(); for(char c : arr){ if(Character.isDigit(c)) number = 10 * number + c - '0'; else if(c == '+'){ result += sign * number; sign = 1; number = 0; } else if(c == '-'){ result += sign * number; sign = -1; number = 0; } else if(c == '('){ st.push(result); st.push(sign); sign = 1; result = 0; } else if(c == ')'){ result += sign * number; result = result * st.pop() + st.pop(); number = 0; } } if(number != 0) result += sign * number; return result; } }
Problem 2:
The expression string contains only '+', '-', '*', '/', non-negative integers and spaces. You may assume the given expression is valid.
e.g. "2+5*6+4/3" return 33
Solution:
1. we use a variable number to store each number, a variable sign to store the sign of current number or expression, a variable multi_result to store the result of * and / operations, a variable result to store the result of this whole expression and a state variable multi_state to determine current state. If multi_state is 0, we are not in a * or / expression now. if state is 1, we are in a * expression and if state is -1, we are in a / expression. Scan the whole string.
2. Whenever we encounter a digit we update number to number*10+c-'0'.
3. Whenever we encounter a + or -, if state is 0, we simply add sign*number to the result. If state is 1, we multiply number to the multi_result and add sign*multi_result to the result. -1 is the similar. Then we update number and multi_result to 0 and put sign to 1(+) or -1(-).
4. Whenever we encounter a * or /, if state is 0, we put number into multi_result. If state is 1, we multiply number with multi_result. -1 the similar. Then we update state and put number to 0.
5. After scanning the whole string, we add the remaining numbers to the result according to the state. The operation is the same as 3. and 4. Then return the result.
Code:
public class Solution { public int calculate(String s) { char[] arr = s.toCharArray(); int result = 0, number = 0, sign = 1, multi_result = 0, multi_state = 0; for(char c : arr){ if(Character.isDigit(c)){ number = number*10+c-'0'; } else if(c == '+' || c == '-'){ if(multi_state != 0){ if(multi_state == 1) multi_result *= number; else multi_result /= number; result += multi_result*sign; } else result += number * sign; number = 0; sign = c=='+'? 1 : -1; multi_state = 0; } else if(c == '*' || c == '/'){ if(multi_state == 0) multi_result = number; else if(multi_state == 1) multi_result *= number; else multi_result /= number; multi_state = c=='*'? 1 : -1; number = 0; } } if(multi_state != 0){ if(multi_state == 1) multi_result *= number; else multi_result /= number; result += sign*multi_result; } else{ result += sign * number; } return result; } }

浙公网安备 33010602011771号