LeetCode 150: Evaluate Reverse Polish Notation Solution

Master LeetCode problem 150 (Evaluate Reverse Polish Notation), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

150. Evaluate Reverse Polish Notation

Problem Explanation

Explanation

To solve this problem, we can use a stack to keep track of the operands and perform operations based on the operators encountered in the input array of tokens. We iterate through each token and if the token is an operator, we pop the last two operands from the stack, perform the operation, and push the result back onto the stack. If the token is an operand, we simply push it onto the stack. At the end, the stack will contain only one element which is the final result of the expression.

Algorithm:

  1. Initialize an empty stack to store operands.
  2. Iterate through each token in the input array of tokens.
  3. If the token is an operator, pop the last two operands from the stack, perform the operation, and push the result back onto the stack.
  4. If the token is an operand, push it onto the stack.
  5. At the end, the stack will contain only one element which is the final result of the expression.

Time Complexity: O(n) where n is the number of tokens in the input array. Space Complexity: O(n) where n is the number of tokens in the input array.

Solution Code

import java.util.Stack;

class Solution {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        
        for(String token : tokens) {
            if(token.equals("+")) {
                int operand2 = stack.pop();
                int operand1 = stack.pop();
                stack.push(operand1 + operand2);
            } else if(token.equals("-")) {
                int operand2 = stack.pop();
                int operand1 = stack.pop();
                stack.push(operand1 - operand2);
            } else if(token.equals("*")) {
                int operand2 = stack.pop();
                int operand1 = stack.pop();
                stack.push(operand1 * operand2);
            } else if(token.equals("/")) {
                int operand2 = stack.pop();
                int operand1 = stack.pop();
                stack.push(operand1 / operand2);
            } else {
                stack.push(Integer.parseInt(token));
            }
        }
        
        return stack.pop();
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 150 (Evaluate Reverse Polish Notation)?

This page provides optimized solutions for LeetCode problem 150 (Evaluate Reverse Polish Notation) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 150 (Evaluate Reverse Polish Notation)?

The time complexity for LeetCode 150 (Evaluate Reverse Polish Notation) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 150 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 150 in Java, C++, or Python.

Back to LeetCode Solutions