Skip to content

Commit

Permalink
Merge pull request #29 from f88083/150-Evaluate-Reverse-Polish-Notation
Browse files Browse the repository at this point in the history
[Solution ]150. Evaluate Reverse Polish Notation in Java from Leetcode
  • Loading branch information
iamdestinychild authored Oct 3, 2023
2 parents 52ab43c + f361d13 commit 0a98c8d
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
5 changes: 5 additions & 0 deletions LeetCode/150-Evaluate-Reverse-Polish-Notation/Java/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# How to Run?

1. Go to the leetcode question site. [Link](https://leetcode.com/problems/evaluate-reverse-polish-notation/)
2. Copy and paste the code.
3. Run or Submit to see the result.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions LeetCode/150-Evaluate-Reverse-Polish-Notation/Java/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public int evalRPN(String[] tokens) {
// Init. stack for the integers
Stack<Integer> stack = new Stack<>();

// Iterate through all the tokens
for (String token : tokens) {
// Check symbol
if (token.equals("+")) {
// Push the result to the stack
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("*")) {
stack.push(stack.pop() * stack.pop());
} else if (token.equals("/")) {
int b = stack.pop();
int a = stack.pop();
stack.push(a / b);
} else { // If it is a number, parse it and push to the stack
stack.push(Integer.parseInt(token));
}
}
// Pop the result
return stack.pop();
}
}
45 changes: 45 additions & 0 deletions LeetCode/150-Evaluate-Reverse-Polish-Notation/PROBLEM.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# [150. Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/description/)

You are given an array of strings tokens that represents an arithmetic expression in a [Reverse Polish Notation](http://en.wikipedia.org/wiki/Reverse_Polish_notation).

Evaluate the expression. Return an integer that represents the value of the expression.

**Note** that:

* The valid operators are `'+'`, `'-'`, `'*'`, and `'/'`.
* Each operand may be an integer or another expression.
* The division between two integers always **truncates toward zero**.
* There will not be any division by zero.
* The input represents a valid arithmetic expression in a reverse polish notation.
* The answer and all the intermediate calculations can be represented in a **32-bit** integer.


## 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].

0 comments on commit 0a98c8d

Please sign in to comment.