注意负数情况的除法
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param tokens string字符串一维数组
# @return int整型
#
class Solution:
def evalRPN(self , tokens: List[str]) -> int:
# write code here
arr = []
for ele in tokens:
if ele[0] == '-' and len(ele)>1:
arr.append(0-int(ele[1:]))
elif ele.isdigit():
arr.append(int(ele))
else:
a1 = arr.pop()
a2 = arr.pop()
if ele == "+":
temp = a1 + a2
elif ele == "-":
temp = a2 - a1
elif ele == "*":
temp = a1 * a2
elif ele == "/":
temp = int(a2/a1) # 负数情况
arr.append(temp)
return arr[0]