#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param tokens string字符串一维数组
# @return int整型
#
class Solution:
def evalRPN(self , tokens: List[str]) -> int:
# write code here
s = []#用于存储操作数
for x in tokens:
if x in '+-*/':#遍历到操作符时
a, b = s.pop(), s.pop()
if x=='+':
s.append(b+a)
elif x=='-':
s.append(b-a)
elif x=='*':
s.append(b*a)
elif x=='/':
s.append(int(b/a))# 取整,四舍五入
else:#操作数入栈
s.append(int(x))
return s[-1]



京公网安备 11010502036488号