-
Notifications
You must be signed in to change notification settings - Fork 15
/
expression-evaluation.java
102 lines (88 loc) · 2.93 KB
/
expression-evaluation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Given an expression string array, return the final result of this expression
// 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, +, -, *, /, (, ).
public class Solution {
/**
* @param expression: an array of strings;
* @return: an integer
*/
public int evaluateExpression(String[] exp) {
int len = exp.length;
if (len == 0) {
return 0;
}
Stack<Integer> vals = new Stack<Integer>();
int[] index = {0};
return helper(exp, index, vals);
}
public int helper(String[] exp, int[] index, Stack<Integer> vals) {
if (index[0] == exp.length) {
return 0;
}
Stack<String> operators = new Stack<String>();
// alwasy start from 1st element after "("
int i = index[0];
for (; i < exp.length; i++) {
if (exp[i].equals(")")) {
// done with current recursion
break;
} else if (isOp(exp[i])) {
operators.push(exp[i]);
} else {
int val = 0;
if (exp[i].equals("(")){
int[] tmp = {i + 1};
val = helper(exp, tmp, vals);
i = tmp[0];
} else {
val = Integer.parseInt(exp[i]);
}
if (!operators.isEmpty() && (operators.peek().equals("*") || operators.peek().equals("/"))) {
int right = val;
int left = vals.pop();
String op = operators.pop();
int res = compute(left, op, right);
vals.push(res);
} else {
// treat "-" as sign rather than operator, since "-" operator is buggy and hard to handle.
if (!operators.isEmpty() && operators.peek().equals("-")) {
val = -val;
}
vals.push(val);
}
}
}
// sum all remaining values
while(!operators.isEmpty()) {
int right = vals.pop();
int left = vals.pop();
operators.pop();
// int res = compute(left, "+", right);
vals.push(left + right);
}
index[0] = i;
return vals.isEmpty()? 0 : vals.pop();
}
public boolean isOp(String s) {
return s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/");
}
public int compute(int left, String op, int right) {
if (op.equals("+")) {
return left + right;
} else if (op.equals("-")) {
return left - right;
} else if (op.equals("*")) {
return left * right;
} else {
return left / right;
}
}
};