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

  • Time: O(n) Space: O(1)
public int evalRPN(String[] tokens) {
    Stack<Integer> stack = new Stack<>();
    for (int i = 0; i < tokens.length; i++) {
        switch (tokens[i]) {
        case "+":
            stack.push(stack.pop() + stack.pop());
            break;
        case "-":
            stack.push(-stack.pop() + stack.pop());
            break;
        case "*":
            stack.push(stack.pop() * stack.pop());
            break;
        case "/":
            int n1 = stack.pop(), n2 = stack.pop();
            stack.push(n2 / n1);
            break;
        default:
            stack.push(Integer.parseInt(tokens[i]));
        }
    }
    return stack.pop();
}

results matching ""

    No results matching ""