forked from yingl/LintCodeInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate_reverse_polish_notation.py
More file actions
31 lines (29 loc) · 1.16 KB
/
evaluate_reverse_polish_notation.py
File metadata and controls
31 lines (29 loc) · 1.16 KB
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
# -*- coding: utf-8 -*-
class Solution:
# @param {string[]} tokens The Reverse Polish Notation
# @return {int} the value
def evalRPN(self, tokens):
# Write your code here
ret = []
for item in tokens:
if self.isOperator(item):
right_val = ret.pop()
left_val = ret.pop()
if item == '*':
ret.append(left_val * right_val)
elif item == '/':
# Python对于正负数相除有问题,需要单独处理
hack = ((left_val > 0) and (right_val < 0)) or ((left_val < 0) and (right_val > 0))
div = abs(left_val) / abs(right_val)
if hack:
div = -1 * int(div)
ret.append(div)
elif item == '+':
ret.append(left_val + right_val)
else:
ret.append(left_val - right_val)
else:
ret.append(int(item))
return 0 if not ret else ret[0]
def isOperator(self, item):
return (item == '+') or (item == '-') or (item == '*') or (item == '/')