Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Evaluate Reverse Polish Notation problem solution

YASH PAL, 31 July 2024

In this Leetcode Evaluate Reverse Polish Notation problem solution, we need to Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, and /. Each operand may be an integer or another expression. Note that the division between two integers should truncate toward zero.

It is guaranteed that the given RPN expression is always valid. That means the expression would always evaluate a result, and there will not be any division by zero operation.

Leetcode Evaluate Reverse Polish Notation problem solution

Problem solution in Python.

import operator

class Solution:
    def evalRPN(self, tokens):
        """
        :type tokens: List[str]
        :rtype: int
        """
        
        if tokens == []:
            return -1
        
        stack = [] 
        
        ops = ["+", "-", "*", "/"]
        ops_lookup = {"+": operator.add, "-": operator.sub, "*": operator.mul, "/": operator.truediv}
        
        for token in tokens:            
            if token not in ops:
                stack.append(int(token))
            else:
                val1 = stack.pop(-1)
                val2 = stack.pop(-1)
                result = ops_lookup[token](val2, val1)
                stack.append(int(result))
            
        return stack.pop()

Problem solution in Java.

public class Solution {
    public int evalRPN(String[] tokens) {
        Deque<Integer> stack = new ArrayDeque();
        for (String s : tokens) {
            if (s.charAt(s.length() - 1) >= '0' && s.charAt(s.length() - 1) <= '9') {
                stack.push(Integer.valueOf(s));
            } else {
                int cur = stack.pop(), pre = stack.pop();
                stack.push(cal(pre, s.charAt(0), cur));
            }
        }
        return stack.pop();
    }
    
    private int cal(int a, char p, int b) {
        switch (p) {
            case '+': return a + b;
            case '-': return a - b;
            case '*': return a * b;
            case '/': return a / b;
        }
        return -1;
    }
}

Problem solution in C++.

class Solution {
public:
    int evalRPN(vector<string>& tokens) {
        stack<int> s;
        for ( const string& token: tokens ) {
            if (token == "+") {
                int rhs = s.top();
                s.pop();
                int lhs = s.top();
                s.pop();
                int res = lhs + rhs;
                s.push(res);
            } else if (token == "-") {
                int rhs = s.top();
                s.pop();
                int lhs = s.top();
                s.pop();
                int res = lhs - rhs;
                s.push(res);
            } else if (token == "/") {
                int rhs = s.top();
                s.pop();
                int lhs = s.top();
                s.pop();
                int res = lhs / rhs;
                s.push(res);
            } else if (token == "*") {
                int rhs = s.top();
                s.pop();
                int lhs = s.top();
                s.pop();
                int res = lhs * rhs;
                s.push(res);
            } else {
                s.push(stoi(token));
            }
        }
        return s.top();
    }
};

Problem solution in C.

int evalRPN(char ** tokens, int tokensSize){
    int s[10000], t = -1, r;
    for (int i = 0 ; i < tokensSize ; i++)
        if (!isdigit(tokens[i][0]) && !tokens[i][1]) {
            int p2 = s[t--], p1 = s[t--];
            switch (tokens[i][0]) {
                case '+': r = p1 + p2; break;
                case '-' : r = p1 - p2; break;
                case '*' : r = p1 * p2; break;
                case '/' : r = p1 / p2; break;
            }
            s[++t] = r;
        } else
            s[++t] = atoi(tokens[i]);
    return s[t--];
}

coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes