Skip to content

Commit e62610b

Browse files
committed
solve problem Evaluate Reverse Polish Notation
1 parent e11919a commit e62610b

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ All solutions will be accepted!
266266
|19|[Remove Nth Node From End Of List](https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/description/)|[java/py/js](./algorithms/RemoveNthNodeFromEndOfList)|Medium|
267267
|526|[Beautiful Arrangement](https://leetcode-cn.com/problems/beautiful-arrangement/description/)|[java/py/js](./algorithms/BeautifulArrangement)|Medium|
268268
|814|[Binary Tree Pruning](https://leetcode-cn.com/problems/binary-tree-pruning/description/)|[java/py/js](./algorithms/BinaryTreePruning)|Medium|
269+
|150|[Evaluate Reverse Polish Notation](https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/description/)|[java/py/js](./algorithms/EvaluateReversePolishNotation)|Medium|
269270

270271
# Database
271272
|#|Title|Solution|Difficulty|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Evaluate Reverse Polish Notation
2+
This problem is easy to solve by stack
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public int evalRPN(String[] tokens) {
3+
LinkedList<String> stack = new LinkedList<String>();
4+
String ops = "+-*/";
5+
6+
for (String token : tokens) {
7+
if (ops.indexOf(token) != -1) {
8+
int value2 = Integer.valueOf(stack.pop()),
9+
value1 = Integer.valueOf(stack.pop());
10+
String value = null;
11+
12+
if (token.equals("+"))
13+
value = String.valueOf(value1 + value2);
14+
else if (token.equals("-"))
15+
value = String.valueOf(value1 - value2);
16+
else if (token.equals("*"))
17+
value = String.valueOf(value1 * value2);
18+
else if (token.equals("/"))
19+
value = String.valueOf(value1 / value2);
20+
stack.push(value);
21+
} else {
22+
stack.push(token);
23+
}
24+
}
25+
26+
return stack.size() > 0 ? Integer.valueOf(stack.getFirst()) : 0;
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {string[]} tokens
3+
* @return {number}
4+
*/
5+
var evalRPN = function(tokens) {
6+
let stack = []
7+
8+
tokens.forEach(token => {
9+
if (['+', '-', '*', '/'].indexOf(token) !== -1) {
10+
let value2 = parseInt(stack.pop()),
11+
value1 = parseInt(stack.pop()),
12+
value = 0
13+
if (token == '+')
14+
value = value1 + value2
15+
else if (token == '-')
16+
value = value1 - value2
17+
else if (token == '*')
18+
value = value1 * value2
19+
else if (token == '/')
20+
value = parseInt(value1 / value2)
21+
stack.push(value)
22+
} else {
23+
stack.push(token)
24+
}
25+
})
26+
27+
return stack.length > 0 ? parseInt(stack[0]) : 0
28+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution(object):
2+
def evalRPN(self, tokens):
3+
"""
4+
:type tokens: List[str]
5+
:rtype: int
6+
"""
7+
stack = []
8+
9+
for token in tokens:
10+
if token in ['+', '-', '*', '/']:
11+
value2 = int(stack.pop())
12+
value1 = int(stack.pop())
13+
value = 0
14+
if token == '+':
15+
value = value1 + value2
16+
elif token == '-':
17+
value = value1 - value2
18+
elif token == '*':
19+
value = value1 * value2
20+
elif token == '/':
21+
value = int(value1 * 1.0 / value2)
22+
stack.append(value)
23+
else:
24+
stack.append(token)
25+
26+
return 0 if len(stack) == 0 else int(stack[0])

0 commit comments

Comments
 (0)