Evaluate Reverse Polish Notation
描述
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are +, -, *, /
. Each operand may be an integer or another expression.
Some examples:
["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
分析
逆波兰表达式是典型的递归结构,所以可以用递归来求解,也可以用栈来求解。
递归版
递归的写法,C++版可以AC,但Java版会爆栈StackOverflowError
,所以Java 只能用栈来解决。
// Evaluate Reverse Polish Notation
// 递归,时间复杂度O(n),空间复杂度O(logn)
// StackOverflowError
class Solution {
public int evalRPN(final String[] tokens) {
return evalRPN(tokens, tokens.length - 1);
}
private static int evalRPN(final String[] tokens, int i) {
if (i < 0) return 0;
int x, y;
final String token = tokens[i--];
if (isOperator(token)) {
y = evalRPN(tokens, i--);
x = evalRPN(tokens, i--);
switch (token.charAt(0)) {
case '+': x += y; break;
case '-': x -= y; break;
case '*': x *= y; break;
default: x /= y;
}
} else {
x = Integer.parseInt(token);
}
return x;
}
private static boolean isOperator(final String op) {
return op.length() == 1 && OPS.indexOf(op) != -1;
}
private static String OPS = new String("+-*/");
}
迭代版
// Max Points on a Line
// 迭代,时间复杂度O(n),空间复杂度O(logn)
class Solution {
public int evalRPN(String[] tokens) {
Stack<String> s = new Stack<>();
for (String token : tokens) {
if (!isOperator(token)) {
s.push(token);
} else {
int y = Integer.parseInt(s.pop());
int x = Integer.parseInt(s.pop());
switch (token.charAt(0)) {
case '+': x += y; break;
case '-': x -= y; break;
case '*': x *= y; break;
default: x /= y;
}
s.push(String.valueOf(x));
}
}
return Integer.parseInt(s.peek());
}
private static boolean isOperator(final String op) {
return op.length() == 1 && OPS.indexOf(op) != -1;
}
private static String OPS = new String("+-*/");
}