Lintcode: Expression Evaluation (Basic Calculator III)

Given an expression string array, return the final result of this expression

Have you met this question in a real interview? Yes
Example
For the expression 2*6-(23+7)/(1+2),
input is

[
  "2", "*", "6", "-", "(",
  "23", "+", "7", ")", "/",
  (", "1", "+", "2", ")"
],
return 2

Note
The expression contains only integer, +, -, *, /, (, ).

这道题其实应该算Basic Calculator III. 参考了http://blog.csdn.net/nicaishibiantai/article/details/45740649

思路就是两个stack,一个存数字一个存符号。如果遇到数字直接存到数字stack;如果遇到符号,有几种情况:

1.当前符号比上一个符号优先级高,比如* 高于+,那么直接进栈

2.当前符号低于上一个,那么就要把所有已经在stack里面优先于当前符号的全算完,再推进当前符号

3.当前符号是“(”,直接push

4.当前符号是“)”,就要把所有“(”以前的符号全部算完

 1 public class Solution {
 2     /**
 3      * @param expression: an array of strings;
 4      * @return: an integer
 5      */
 6     public int evaluateExpression(String[] expression) {
 7         // write your code here
 8         Stack<Integer> integers = new Stack<Integer>();
 9         Stack<String> ops = new Stack<String>();
10         int i = 0;
11         while (i < expression.length) {
12             String cur = expression[i];
13             if (isOp(cur)) { // current string is an op
14                 if (cur.equals("(")) ops.push(cur);
15                 else if (cur.equals(")")) {
16                     while (ops.size()>0 && !ops.peek().equals("(")) {
17                         integers.push(calc(integers.pop(), integers.pop(), ops.pop()));
18                     }
19                     ops.pop();
20                 }
21                 else { // +,-,*,/
22                     while (ops.size()>0 && precede(cur, ops.peek())) {
23                         integers.push(calc(integers.pop(), integers.pop(), ops.pop()));
24                     }
25                     ops.push(cur);
26                 }
27             }
28             else integers.push(Integer.parseInt(cur)); // current String is an integer, push to integer stack
29             i++;
30         }
31         
32         while (!ops.isEmpty()) {
33             integers.push(calc(integers.pop(), integers.pop(), ops.pop()));
34         }
35         return integers.isEmpty()? 0 : integers.pop();
36     }
37     
38     public boolean isOp(String input) {
39         if (input.equals("+") || input.equals("-") || input.equals("*")
40          || input.equals("/") || input.equals("(") || input.equals(")"))
41             return true;
42         return false;
43     }
44     
45     public int calc(int a, int b, String op) {
46         if (op.equals("+")) return a+b;
47         else if (op.equals("-")) return b-a;
48         else if (op.equals("*")) return a*b;
49         else return b/a;
50     }
51     
52     public boolean precede(String a, String b) {
53         if (b.equals("*") || b.equals("/")) return true;
54         if (b.equals("+") || b.equals("-")) {
55             if (a.equals("*") || a.equals("/")) return false;
56             else return true;
57         }
58         return false; //case like (a+b) 到第一个+号时,+和(比应该return false
59     }
60 };
原文地址:https://www.cnblogs.com/EdwardLiu/p/5177272.html