|
1 | 1 | package com.leetcode.stacks;
|
2 | 2 |
|
| 3 | +import java.util.Stack; |
| 4 | + |
| 5 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 6 | + |
3 | 7 | /**
|
| 8 | + * Level: Medium |
| 9 | + * Problem Link: https://leetcode.com/problems/evaluate-reverse-polish-notation |
| 10 | + * Problem Description: |
| 11 | + * Evaluate the value of an arithmetic expression in Reverse Polish Notation. |
| 12 | + * <p> |
| 13 | + * Valid operators are +, -, *, /. Each operand may be an integer or another expression. |
| 14 | + * <p> |
| 15 | + * Note: |
| 16 | + * Division between two integers should truncate toward zero. |
| 17 | + * The given RPN expression is always valid. That means the expression would always evaluate to a result and there |
| 18 | + * won't be any divide by zero operation. |
| 19 | + * <p> |
| 20 | + * Example 1: |
| 21 | + * Input: ["2", "1", "+", "3", "*"] |
| 22 | + * Output: 9 |
| 23 | + * Explanation: ((2 + 1) * 3) = 9 |
| 24 | + * <p> |
| 25 | + * Example 2: |
| 26 | + * Input: ["4", "13", "5", "/", "+"] |
| 27 | + * Output: 6 |
| 28 | + * Explanation: (4 + (13 / 5)) = 6 |
| 29 | + * <p> |
| 30 | + * Example 3: |
| 31 | + * Input: ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"] |
| 32 | + * Output: 22 |
| 33 | + * Explanation: |
| 34 | + * ((10 * (6 / ((9 + 3) * -11))) + 17) + 5 |
| 35 | + * = ((10 * (6 / (12 * -11))) + 17) + 5 |
| 36 | + * = ((10 * (6 / -132)) + 17) + 5 |
| 37 | + * = ((10 * 0) + 17) + 5 |
| 38 | + * = (0 + 17) + 5 |
| 39 | + * = 17 + 5 |
| 40 | + * = 22 |
| 41 | + * |
4 | 42 | * @author rampatra
|
5 | 43 | * @since 2019-07-27
|
6 | 44 | */
|
7 | 45 | public class ReversePolishNotation {
|
| 46 | + |
| 47 | + /** |
| 48 | + * Time Complexity: |
| 49 | + * Space Complexity: |
| 50 | + * Runtime: <a href="https://leetcode.com/submissions/detail/246794713/">5 ms</a>. |
| 51 | + * |
| 52 | + * @param tokens |
| 53 | + * @return |
| 54 | + */ |
| 55 | + public static int evalRPN(String[] tokens) { |
| 56 | + int operand1; |
| 57 | + int operand2; |
| 58 | + |
| 59 | + Stack<Integer> stack = new Stack<>(); |
| 60 | + |
| 61 | + for (String s : tokens) { |
| 62 | + switch (s) { |
| 63 | + case "+": |
| 64 | + stack.push(stack.pop() + stack.pop()); |
| 65 | + break; |
| 66 | + case "-": |
| 67 | + operand1 = stack.pop(); |
| 68 | + operand2 = stack.pop(); |
| 69 | + stack.push(operand2 - operand1); |
| 70 | + break; |
| 71 | + case "*": |
| 72 | + stack.push(stack.pop() * stack.pop()); |
| 73 | + break; |
| 74 | + case "/": |
| 75 | + operand1 = stack.pop(); |
| 76 | + operand2 = stack.pop(); |
| 77 | + stack.push(operand2 / operand1); |
| 78 | + break; |
| 79 | + default: |
| 80 | + stack.push(Integer.parseInt(s)); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return stack.pop(); |
| 85 | + } |
| 86 | + |
| 87 | + public static void main(String[] args) { |
| 88 | + assertEquals(18, evalRPN(new String[]{"18"})); |
| 89 | + assertEquals(9, evalRPN(new String[]{"2", "1", "+", "3", "*"})); |
| 90 | + assertEquals(6, evalRPN(new String[]{"4", "13", "5", "/", "+"})); |
| 91 | + assertEquals(22, evalRPN(new String[]{"10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"})); |
| 92 | + } |
8 | 93 | }
|
0 commit comments