题解 | #逆波兰表达式求值#
逆波兰表达式求值
https://www.nowcoder.com/practice/885c1db3e39040cbae5cdf59fb0e9382
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param tokens string字符串一维数组 # @return int整型 # class Solution: def evalRPN(self , tokens: List[str]) -> int: #2+1=3 3*4=12 # write code here list1=[] for char in tokens: if char in '+-*/': a=list1.pop() b=list1.pop() if char=='+': list1.append(a+b) elif char=='-': list1.append(b-a) elif char=='*': list1.append(a*b) else: list1.append(int(b/a)) else: list1.append(int(char)) return list1[0] # list1=[] # for char in tokens: # if char=='+'and len(list1)>=2: # a=list1.pop() # b=list1.pop() # result=a+b # list1.append(result) # elif char=='-' and len(list1)>=2: # a=list1.pop() # b=list1.pop() # result=b-a # list1.append(result) # elif char=='*'and len(list1)>=2: # a=list1.pop() # b=list1.pop() # result=a*b # list1.append(result) # elif char=='/' and len(list1)>=2: # a=list1.pop() # b=list1.pop() # result=a/b # list1.append(result) # else: # list1.append(int(char)) # return list1[0]