-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path150. Evaluate Reverse Polish Notation.py
47 lines (41 loc) · 1.28 KB
/
150. Evaluate Reverse Polish Notation.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
39
40
41
42
43
44
45
46
47
class Solution:
def evalRPN(self, tokens):
tokens.reverse()
int_stack = []
opreation = ['+','-','*','/']
while tokens:
ch = tokens.pop()
if ch not in opreation:
# ch is a number
int_stack.append(int(ch))
else:
opt = ch
num2 = int_stack.pop()
num1 = int_stack.pop()
if opt == '+':
ans = num1+num2
elif opt == '-':
ans = num1-num2
elif opt == '*':
ans = num1*num2
elif opt == '/':
# if num1*num2<0:
# ans = ceil(num1/num2)
# else:
# ans = num1//num2
ans = int(num1/num2)
int_stack.append(ans)
return int_stack.pop()
import time
start = time.clock()
nums = ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]
ExpectOutput = 22
solu = Solution() # 先对类初始化,才能进行调用
temp = solu.evalRPN(nums)
if (temp == ExpectOutput):
print('right')
else:
print('wrong')
print(temp)
elapsed = (time.clock() - start)
print("Time used:", elapsed)