## 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


### 递归版

// 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("+-*/");
}