跳到主要内容

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
// Recursive
// Time Complexity: O(n),Space Complexity: O(n)
class Solution {
private int top;

public int evalRPN(String[] tokens) {
top = tokens.length-1;
return dfs(tokens);
}

public int dfs(String[] tokens) {
String token = tokens[top--];
if (!"+-*/".contains(token)) {
return Integer.parseInt(token);
} else {
int y = dfs(tokens);
int x = dfs(tokens);
switch (token) {
case "+": return x + y;
case "-": return x - y;
case "*": return x * y;
default: return x / y;
}
}
}
}

// Evaluate Reverse Polish Notation
// UsingStack
// Time Complexity: O(n),Space Complexity: O(n)
class Solution {
public int evalRPN(String[] tokens) {
Stack<Integer> s = new Stack<>();
for (String token : tokens) {
if (!"+-*/".contains(token)) {
s.push(Integer.valueOf(token));
} else {
int y = s.pop();
int x = s.pop();
switch (token) {
case "+": x += y; break;
case "-": x -= y; break;
case "*": x *= y; break;
default: x /= y;
}
s.push(x);
}
}
return s.peek();
}
}