227. Basic Calculator II
http://www.cnblogs.com/grandyang/p/4601208.html 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. Example 1: Input: "3+2*2" Output: 7 Example 2: Input: " 3/2 " Output: 1 Example 3: Input: " 3+5 / 2 " Output: 5 Use stack to solve this question Solution 1 : use 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; } } Solution 2: without using stack class Solution { public int calculate(String s) { // remove all empty space s = s.replaceAll(" ", ""); int sum = s.charAt(0) - '0'; int prev = s.charAt(0) - '0'; int index = 0; char op = '+'; while(index + 1 < s.length() && Character.isDigit(s.charAt(index+1))){ sum = sum * 10 + (s.charAt(index + 1) - '0'); prev = prev * 10 + (s.charAt(index + 1) - '0'); index++; } for(int i = index + 1; i < s.length(); i++){ char c = s.charAt(i); if(!Character.isDigit(c)){ op = c; }else{ int current = c - '0'; while(i + 1 < s.length() && Character.isDigit(s.charAt(i + 1))){ current = current * 10 + (s.charAt(i + 1) - '0'); i++; } if(op == '+'){ sum = sum + current; prev = current; } if(op == '-'){ sum = sum - current; prev = -current; } if(op == '*'){ sum = sum - prev + prev * current; prev = prev * current; } if(op == '/'){ sum = sum - prev + prev / current; prev = prev / current; } } } return sum; } }
posted on 2018-11-06 08:19 猪猪🐷 阅读(237) 评论(0) 收藏 举报
浙公网安备 33010602011771号