772. 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-negative integers 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 Generic solution: Use two stacks when encounter parentheses https://leetcode.com/problems/basic-calculator-iii/discuss/113600/Java-O(n)-Solution-Using-Two-Stacks prec·e·dence the condition of being considered more important than someone or something else; priority in importance, order, or rank. This solution is clear and easy to generalize to calculator I and II. Less tricks actually makes debugging much less confusing. From my understanding, the key point to this solution are: 1. always putting the high precedence operands on the right side of stack. If we encountered a new operands with lower precedence, them we know we can compute previous operand. 2. the evaluation of operand is not triggered by new number, but new operand Below are my python version: Comment on this https://leetcode.com/problems/basic-calculator-iii/discuss/113600/Java-O(n)-Solution-Using-Two-Stacks 不是我写的, 有些地方还不懂 class Solution { public int calculate(String s) { if (s == null || s.length() == 0) return 0; Stack<Integer> nums = new Stack<>(); // the stack that stores numbers Stack<Character> ops = new Stack<>(); // the stack that stores operators (including parentheses) int num = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == ' ') continue; if (Character.isDigit(c)) { num = c - '0'; // iteratively calculate each number while (i < s.length() - 1 && Character.isDigit(s.charAt(i+1))) { num = num * 10 + (s.charAt(i+1) - '0'); i++; } nums.push(num); num = 0; // reset the number to 0 before next calculation } else if (c == '(') { ops.push(c); } else if (c == ')') { // do the math when we encounter a ')' until '(' while (ops.peek() != '(') nums.push(operation(ops.pop(), nums.pop(), nums.pop())); ops.pop(); // get rid of '(' in the ops stack // ? 不知道这个precedence 在做什么? } else if (c == '+' || c == '-' || c == '*' || c == '/') { while (!ops.isEmpty() && precedence(c, ops.peek())) nums.push(operation(ops.pop(), nums.pop(),nums.pop())); ops.push(c); } } while (!ops.isEmpty()) { nums.push(operation(ops.pop(), nums.pop(), nums.pop())); } return nums.pop(); } private static int operation(char op, int b, int a) { switch (op) { case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': return a / b; // assume b is not 0 } return 0; } // helper function to check precedence of current operator and the uppermost operator in the ops stack // 这个 helper function, 不懂在做什么 private static boolean precedence(char op1, char op2) { if (op2 == '(' || op2 == ')') return false; if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-')) return false; return true; } }
posted on 2018-11-06 08:18 猪猪🐷 阅读(268) 评论(0) 收藏 举报
浙公网安备 33010602011771号