Skip to content

Commit d4f5f17

Browse files
author
Partho Biswas
committed
150. Evaluate Reverse Polish Notation
1 parent fda5163 commit d4f5f17

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ I have solved quite a number of problems from several topics. See the below tabl
249249
|04| [394. Decode String](https://leetcode.com/problems/decode-string/)| [Python](https://tinyurl.com/wu6rdaw/394_Decode_String.py)| --- | Medium | 📌 Classic stack problem |
250250
|05| [239. Sliding Window Maximum](https://tinyurl.com/rvhujl5)| [Python](https://tinyurl.com/wu6rdaw/239_Sliding_Window_Maximum.py)| **[Video 1](https://tinyurl.com/v767bl3)**, [Official](https://tinyurl.com/www4np2), [Art 1](https://tinyurl.com/y8nbroux), [Art 2](https://tinyurl.com/s62fzls), **[Art 3](https://tinyurl.com/vd3dtch)**, **[Art 4](https://tinyurl.com/yx3sjp46)** | Hard | 📌 Can be solved using Heap, Deque and DP |
251251
|06| [739. Daily Temperatures](https://tinyurl.com/ybbezzmt)| [Python](https://tinyurl.com/wu6rdaw/739_Daily_Temperatures.py)| **[Video 1](https://tinyurl.com/sdbkh3z)**, **[Video 2](https://tinyurl.com/rbvaozl)**, [Art 1](https://tinyurl.com/ss3d9tx) | Medium | 📌 TODO: Check again. A tricky one |
252+
|07| **[150. Evaluate Reverse Polish Notation](https://tinyurl.com/y2xrje8v)** | [Python](https://tinyurl.com/wu6rdaw/150_Evaluate_Reverse_Polish_Notation.py), [Swift](https://tinyurl.com/wuja3c4/150_Evaluate_Reverse_Polish_Notation.swift)| [Vid 1](https://tinyurl.com/s92fp4r) | Medium | --- |
252253

253254
</p>
254255
</details>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# https://tinyurl.com/r35ffgt
2+
from collections import deque
3+
class Solution(object):
4+
def evalRPN(self, tokens):
5+
"""
6+
:type tokens: List[str]
7+
:rtype: int
8+
"""
9+
tokensDeque = deque(tokens)
10+
stack = []
11+
opes = ["+", "-", "*", "/"]
12+
while tokensDeque:
13+
currentToken = tokensDeque.popleft()
14+
if currentToken in opes:
15+
firstNum = int(stack.pop())
16+
secondNum = int(stack.pop())
17+
result = 0
18+
if currentToken == "+":
19+
result = secondNum + firstNum
20+
elif currentToken == "-":
21+
result = secondNum - firstNum
22+
elif currentToken == "*":
23+
result = secondNum * firstNum
24+
else:
25+
if firstNum*secondNum < 0 and secondNum%firstNum != 0:
26+
result = secondNum // firstNum + 1
27+
else:
28+
result = secondNum // firstNum
29+
stack.append(str(result))
30+
else:
31+
stack.append(currentToken)
32+
return stack[-1]
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import Foundation
2+
3+
class Solution {
4+
func evalRPN(_ tokens: [String]) -> Int {
5+
var opes = ["+", "-", "*", "/"]
6+
var stack = [String]()
7+
for token in tokens {
8+
if opes.contains(token) {
9+
var rightOperand: Double! = Double(stack.popLast()!)
10+
var leftOperand: Double! = Double(stack.popLast()!)
11+
var result = 0.0
12+
if token == "+" {
13+
result = leftOperand + rightOperand
14+
} else if token == "-" {
15+
result = leftOperand - rightOperand
16+
} else if token == "*" {
17+
result = leftOperand * rightOperand
18+
} else {
19+
if (rightOperand*leftOperand < 0) && (Int(leftOperand)%Int(rightOperand) != 0) {
20+
result = leftOperand / rightOperand + 1
21+
} else {
22+
result = leftOperand / rightOperand
23+
}
24+
}
25+
var res = Int(result.round(.towardZero)
26+
stack.append(String(res))
27+
} else {
28+
stack.append(token)
29+
}
30+
}
31+
print("stack: \(stack)")
32+
return Int(stack.popLast()!)!
33+
}
34+
}

0 commit comments

Comments
 (0)