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
分析
逆波兰表达式是典型的递归结构,所以可以用递归来求解,也可以用栈来求解。
代码
递归版
- Java
- C++
// 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(vector<string>& tokens) {
const string& token = tokens.back(); tokens.pop_back();
if (string("+-*/").find(token) == string::npos) {
return std::stoi(token);
} else {
int y = evalRPN(tokens);
int x = evalRPN(tokens);
switch(token[0]) {
case '+' : return x + y;
case '-' : return x - y;
case '*' : return x * y;
default: return x / y;
}
}
}
};
栈
- Java
- C++
// 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();
}
}
// Evaluate Reverse Polish Notation
// UsingStack
// Time Complexity: O(n),Space Complexity: O(n)
class Solution {
public:
int evalRPN(vector<string> &tokens) {
stack<int> s;
for (auto token : tokens) {
if (string("+-*/").find(token) == string::npos) {
s.push(std::stoi(token));
} else {
int y = s.top(); s.pop();
int x = s.top(); s.pop();
switch(token[0]) {
case '+' : x += y; break;
case '-' : x -= y; break;
case '*' : x *= y; break;
default: x /= y;
}
s.push(x);
}
}
return s.top();
}
};