-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from f88083/150-Evaluate-Reverse-Polish-Notation
[Solution ]150. Evaluate Reverse Polish Notation in Java from Leetcode
- Loading branch information
Showing
4 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
LeetCode/150-Evaluate-Reverse-Polish-Notation/Java/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]. |