### This solution will TimeoutclassSolution(object):defcalculate(self,s):""" :type s: str :rtype: int """seq=re.split('(\D)',s)operator=[]nums=[]stack=[]i=0whilei<len(seq):ifseq[i]==" ":i+=1continue# numberifseq[i]notin"+-*/":stack.append(int(seq[i]))i+=1# operator: */ will handle the calculation hereifseq[i]in"*/":left=stack.pop()right=int(seq[i+1])ifseq[i]=="*":ret=left*rightelse:ret=left/rightstack.append(ret)i+=2else:# "+-"stack.append(seq[i])i+=1# handle +- herewhilelen(stack)>1:left=stack.pop(0)op=stack.pop(0)right=stack.pop(0)ifop=="+":stack.insert(left+right)else:stack.insert(left-right)returnstack[0]classSolution(object):defcalculate(self,s):""" :type s: str :rtype: int """s=re.sub(r'\d+',' \g<0> ',s)seq=s.split()idx=0total=0d=0last_op="+"whileidx<len(seq):# numbersifseq[idx]notin"*/+-":d=int(seq[idx])# calculate "*" and "/" immediatelyifseq[idx]in"*/":ifseq[idx]=="*":d=d*int(seq[idx+1])ifseq[idx]=="/":d=d/int(seq[idx+1])idx+=1# only interest in last_opifseq[idx]in"+-":iflast_op=="+":total+=diflast_op=="-":total-=dlast_op=seq[idx]idx+=1iflast_op=="+":returntotal+delse:returntotal-d