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 supposeb = stack.pop() - the first element we pop
anda = stack.pop() - the second element we pop
and now we dividea/b
and push it back to our stack.When we see a
/
operator, we need to pop out top 2 elements, but supposeb = stack.pop() - the first element we pop
anda = stack.pop() - the second element we pop
and now we subtracta-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();
}
}
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
Github Repository:
Rohithv07 / LeetCodeTopInterviewQuestions
Leetcode Top Interview questions discussed in Leetcode. https://leetcode.com/explore/interview/card/top-interview-questions
LeetCodeTopInterviewQuestions
Leetcode Top Interview questions discussed in Leetcode. https://leetcode.com/explore/interview/card/top-interview-questions-easy/
Also Question answered from CodeSignal.com : https://app.codesignal.com/
Top comments (1)
there is a typo.
explanation for subtraction and division are swapped
otherwise, thanks for your explanation! very helpful!