算术表达式求值
在leetcode上看到了这个问题,想着大学的时候做过一次,应该轻松搞定。开始敲代码才发现竟然忘了个七七八八,这简直不能接受!于是网上搜了一下,稍微理解了一下,再做了一次。
写这篇博客的目的是不想第二次忘记这个问题的解法,中间有些东西抄了百度百科的,本文可以随意使用。
表达是求值问题可以看作是将中缀式转化为后缀式的问题。
(以下算法思想抄的百度百科...... )
将中缀表达式转换为后缀表达式的算法思想:
·开始扫描;
·数字时,加入后缀表达式;
·运算符:
a. 若为 '(',入栈;
b. 若为 ')',则依次把栈中的的运算符加入后缀表达式中,直到出现'(',从栈中删除'(' ;
c. 若为 除括号外的其他运算符, 当其优先级高于除'('以外的栈顶运算符时,直接入栈。否则从栈顶开始,依次弹出比当前处理的运算符优先级高和优先级相等的运算符,直到栈頂是一个比它优先级低的或者是一个左括号为止。
·当扫描的中缀表达式结束时,栈中的的所有运算符出栈;
后缀表达式可以看作是一棵树,(例如231+-4x对应于2-(3+1)*4)运算符只能作为非叶子节点,数字只能作为叶子节点,根节点为最后一个运算符,根节点前面两个字符分别是左儿子和右儿子,依次类推。
用python实现的代码如下:
1 class Solution: 2 # @param {string} s 3 # @return {integer} 4 def calculate(self, s): 5 def add(a,b): 6 return b+a 7 def sub(a,b): 8 return b-a 9 def mul(a,b): 10 return b*a 11 def div(a,b): 12 return b/a 13 14 operater='+-*/()' 15 map = {'+':0,'-':0,'(':-1,'*':1,'/':1} 16 mapFun = {'+':add,'-':sub,'*':mul,'/':div} 17 formatedS = [] 18 flag = False 19 start = 0 20 end = -1 21 stack1=[] 22 stack2=[] 23 result=0 24 #process input string, result in a list of operators and 25 #operands like [1,'+',2] 26 if len(s) == 0: 27 return 0 28 for i in range(0,len(s)): 29 if ' ' == s[i]: 30 if flag: 31 flag = False 32 end = i 33 formatedS.append(int(s[start:end])) 34 continue 35 if s[i].isdigit(): 36 if False == flag: 37 start = i 38 flag = True 39 continue 40 if -1 != operater.find(s[i]): 41 if flag: 42 flag = False 43 end = i 44 formatedS.append(int(s[start:end])) 45 formatedS.append(s[i]) 46 if flag == True: 47 end=len(s) 48 formatedS.append(int(s[start:end])) 49 50 #process the list of operands and operators 51 for item in formatedS: 52 if type(item) == int: 53 stack1.append(item) 54 continue 55 if ')' == item: 56 #if the operator is ')', do all the calculation inside '(' and ')' 57 while len(stack2)>0 and stack2[-1] != '(': 58 stack1.append(mapFun[stack2.pop()](stack1.pop(),stack1.pop())) 59 stack2.pop() 60 continue 61 if '(' == item: 62 stack2.append(item) 63 continue 64 while len(stack2) > 0 and map[item] <= map[stack2[-1]]: 65 #calculate the operators with higher priority first 66 stack1.append(mapFun[stack2.pop()](stack1.pop(),stack1.pop())) 67 stack2.append(item) 68 while len(stack2) > 0: 69 stack1.append(mapFun[stack2.pop()](stack1.pop(),stack1.pop())) 70 result = stack1.pop() 71 return result
浙公网安备 33010602011771号