DEV Community

Cover image for Leetcode 150. Evaluate Reverse Polish Notation
Rohith V
Rohith V

Posted on

Leetcode 150. Evaluate Reverse Polish Notation

Problem Statement

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 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 to a result, and there will not be any division by zero operation.

Reverse Polish notation (RPN), also known as Polish postfix notation or simply postfix notation, is a mathematical notation in which operators follow their operands, in contrast to Polish notation (PN), in which operators precede their operands.

Sample Test Cases

Example 1:

Input: tokens = ["2","1","+","3","*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9
Example 2:

Input: tokens = ["4","13","5","/","+"]
Output: 6
Explanation: (4 + (13 / 5)) = 6
Example 3:

Input: tokens = ["10","6","9","3","+","-11","","/","","17","+","5","+"]
Output: 22
Explanation: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22

Constraints

  • 1 <= tokens.length <= 104
  • tokens[i] is either an operator: "+", "-", "*", or "/", or an integer in the range [-200, 200].

Approach :

The approach is quite straight forward by using stack.

  • Here whenever we see a digit, we simply put it inside our stack.

  • When we see a + operator, we simply pop out top 2 elements, and just add them and put it back into the stack.

  • When we see a * operator, we simply pop out top 2 elements, and just multiply them and put it back into the stack.

  • When we see a - operator, we need to pop out top 2 elements, but suppose b = stack.pop() - the first element we pop and a = stack.pop() - the second element we pop and now we divide a/b and push it back to our stack.

  • When we see a / operator, we need to pop out top 2 elements, but suppose b = stack.pop() - the first element we pop and a = stack.pop() - the second element we pop and now we subtract a-b and push it back to our stack.

Code

class Solution {
    public int evalRPN(String[] tokens) {
        if (tokens.length == 0 || tokens == null)
            return -1;
        Stack<Integer> stack = new Stack<>();
        for (String token : tokens) {
            if (token.equals("+")) {
                stack.push(stack.pop() + stack.pop());
            }
            else if (token.equals("*")) {
                stack.push(stack.pop() * stack.pop());
            }
            else if (token.equals("-")) {
                int b = stack.pop();
                int a = stack.pop();
                stack.push(a - b);
            }
            else if (token.equals("/")) {
                int b = stack.pop();
                int a = stack.pop();
                stack.push(a / b);
            }
            else {
                stack.push(Integer.valueOf(token));
            }
        }
        return stack.pop();
    }
}
Enter fullscreen mode Exit fullscreen mode

Time Complexity and Space Complexity:

O(lengthOfInputArray) time and O(lengthOfInputArray) space.

Code walkthrough with test cases

Input = ["10","6","9","3","+","-11","","/","","17","+","5","+"]
Answer = 22

2

4

Github Repository:

GitHub logo Rohithv07 / LeetCodeTopInterviewQuestions

Leetcode Top Interview questions discussed in Leetcode. https://leetcode.com/explore/interview/card/top-interview-questions




GitHub logo Rohithv07 / LeetCode

LeetCode problems that are solved.

LeetCode

LeetCode problems that are solved.

  • take the bit of the ith(from right) digit:

    bit = (mask >> i) & 1;

  • set the ith digit to 1: mask = mask | (1 << i);

  • set the ith digit to 0: mask = mask & (~(1 << i));

A Sample Structure




Top comments (1)

Collapse
 
toophatjones profile image
tooPhatJones

there is a typo.
explanation for subtraction and division are swapped
otherwise, thanks for your explanation! very helpful!