Question Definition
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 Solution
public int evalRPN(String[] tokens) {
Deque<Integer> stack = new ArrayDeque<>();
for(String token : tokens) {
if(token.equals("+")){
stack.push(stack.pop() + stack.pop());
} else if(token.equals("-")){
int temp = stack.pop();
stack.push(stack.pop() - temp);
} else if(token.equals("*")){
stack.push(stack.pop() * stack.pop());
} else if(token.equals("/")){
int temp = stack.pop();
stack.push(stack.pop() / temp);
} else
stack.push(Integer.parseInt(token));
}
return stack.pop();
}
Comments