-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path150.py
38 lines (36 loc) · 1.21 KB
/
150.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution(object):
def evalRPN(self, tokens):
"""
:type tokens: List[str]
:rtype: int
"""
if len(tokens)==0:
return None
s = []
for i in range(len(tokens)):
if tokens[i]=="+":
e1 = int(s.pop())
e2 = int(s.pop())
s.append(str(e1+e2))
#pass
elif tokens[i]=="-":
e1 = int(s.pop())
e2 = int(s.pop())
s.append(str(e2-e1))
elif tokens[i]=="*":
e1 = int(s.pop())
e2 = int(s.pop())
s.append(str(e1*e2))
elif tokens[i]=="/":
e1 = int(s.pop())
e2 = int(s.pop())
s.append(str(int(e2/e1)))
else:
s.append(tokens[i])
return int(s[0])
if __name__ == "__main__":
s = Solution()
#print(s.evalRPN(["2", "1", "+", "3", "*"]))
#print(s.evalRPN(["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]))
#print(s.evalRPN(["10","6","9","3","+","-11","*","/","*","17","+","5","+"]))
print(s.evalRPN(["10","6","9","3","+","-11","*","/","*","17","+","5","+"]))