題目:java
Implement a basic calculator to evaluate a simple expression string.express
The expression string contains only non-negative integers, +
, -
, *
, /
operators and empty spaces . The integer division should truncate toward zero.app
You may assume that the given expression is always valid.函數
Some examples:post
"3+2*2" = 7 " 3/2 " = 1 " 3+5 / 2 " = 5
Note: Do not use the eval
built-in library function.ui
連接: http://leetcode.com/problems/basic-calculator-ii/lua
7/23/2017spa
6%...code
很僵化的只會先infix to postfix,再來計算blog
注意:
1. string去掉white space: s.replaceAll("\\s+", "")
2. 各類類型的轉換, String <--> Integer, String <--> Character...
3. Stack, Queue的接口函數,已經Queue的2套函數都要熟練
4. infix到postfix如何轉換?運算數直接輸出,運算符要跟stack最上面判斷,若是stack top的優先級高或者有相同優先級,要把當前top的運算符輸出。不管當前是否有更高的優先級,當前運算符必需要加到stack裏面去,由於第二個運算數尚未處理到。最後要把最後一個運算數和全部stack裏的運算符輸出。
5. 運算postfix很簡單,遇到運算數放到stack裏,遇到運算符取出stack最上面2個運算數,將結果壓回stack。最後stack中只剩下一個數,即爲結果。
1 public class Solution { 2 public int calculate(String s) { 3 // remove white spaces from input string 4 s = s.replaceAll("\\s+",""); 5 Stack<String> operators = new Stack<>(); 6 Queue<String> postfix = new LinkedList<>(); 7 8 StringBuilder operand = new StringBuilder(); 9 for (int i = 0; i < s.length(); i++) { 10 char c = s.charAt(i); 11 // build operands from one or more characters 12 if (c >= '0' && c <= '9') { 13 operand.append(c); 14 } else { 15 // put last operand to output 16 postfix.offer(operand.toString()); 17 operand = new StringBuilder(); 18 // add operator to operators stack 19 if (!operators.isEmpty()) { 20 String prevOperator; 21 while (!operators.isEmpty()) { 22 prevOperator = operators.peek(); 23 // if operator on top of stack has same or higher precedence, move top to output 24 if (prevOperator.equals("*") || prevOperator.equals("/") || c == '+' || c == '-') { 25 postfix.offer(operators.pop()); 26 } else { 27 break; 28 } 29 } 30 } 31 // add current operator to operators stack, because the second operand is not visited yet 32 operators.push(Character.toString(c)); 33 } 34 } 35 // last operand not in postfix yet 36 postfix.offer(operand.toString()); 37 // put all operators stack to postfix 38 while (!operators.isEmpty()) { 39 postfix.offer(operators.pop()); 40 } 41 // calculate postfix 42 Stack<Integer> operandStack = new Stack<>(); 43 while (!postfix.isEmpty()) { 44 String o = postfix.poll(); 45 if (o.equals("+") || o.equals("-") || o.equals("*") || o.equals("/")) { 46 Integer operand2 = operandStack.pop(); 47 Integer operand1 = operandStack.pop(); 48 Integer result; 49 if (o.equals("+")) { 50 result = operand1 + operand2; 51 } else if (o.equals("-")) { 52 result = operand1 - operand2; 53 } else if (o.equals("*")) { 54 result = operand1 * operand2; 55 } else { 56 result = operand1 / operand2; 57 } 58 operandStack.push(result); 59 } else { 60 operandStack.push(Integer.valueOf(o)); 61 } 62 } 63 return operandStack.peek(); 64 } 65 }
別人的答案
高優先級直接運算,低優先級存運算數
https://discuss.leetcode.com/topic/16935/share-my-java-solution
看不太懂,看解釋會好一點
https://discuss.leetcode.com/topic/16807/17-lines-c-easy-20-ms
發現你們都是按照第一種作法來的,直接將中間值保存起來,這個都沒有用到stack
將operand, operator放到2個不一樣的stack裏
有詳細解釋
更多討論
https://discuss.leetcode.com/category/235/basic-calculator-ii