Skip to content

Commit

Permalink
计算逆波兰表达式
Browse files Browse the repository at this point in the history
  • Loading branch information
rinwf committed Oct 28, 2018
1 parent c6debea commit f629be7
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lc150. Evaluate Reverse Polish Notation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution:
def evalRPN(self, tokens):
"""
:type tokens: List[str]
:rtype: int
"""
operators = {'+', '-', '*', '/'}
oprd = []

for tk in tokens:
if tk in operators:
b = oprd.pop()
a = oprd.pop()
if tk == '+':
c = a + b
elif tk == '-':
c = a - b
elif tk == '*':
c = a * b
else:
c = int(a / b)
oprd.append(c)
else:
oprd.append(int(tk))
return oprd[0]


tokens = ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]
s = Solution()
print(s.evalRPN(tokens))

0 comments on commit f629be7

Please sign in to comment.