forked from yingl/LintCodeInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_expression_to_reverse_polish_notation.py
More file actions
30 lines (27 loc) · 1.12 KB
/
convert_expression_to_reverse_polish_notation.py
File metadata and controls
30 lines (27 loc) · 1.12 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
# coding: utf-8
class Solution:
# @param expression: A string list
# @return: The Reverse Polish notation of this expression
def convertToRPN(self, expression):
# write your code here
# 原理请参考expression_evaluation.py的注释
op_rank = {'+': 1, '-':1, '*':2, '/': 2, '(': 3, ')': 3}
ret = []
op_stack = []
for item in expression:
if self.isOperand(item):
ret.append(item)
elif item == ')':
while op_stack and op_stack[-1] != '(':
ret.append(op_stack.pop())
op_stack.pop()
else:
while op_stack and (op_stack[-1] != '(') and (op_rank[item] <= op_rank[op_stack[-1]]):
ret.append(op_stack.pop())
op_stack.append(item)
while op_stack:
ret.append(op_stack.pop())
return ret
def isOperand(self, item): # 这里偷个懒,测试数据没有负数!
return (item[0] >= '0') and (item[0] <= '9')
# hard: http://lintcode.com/problem/convert-expression-to-reverse-polish-notation