任务描述
实现代码
class Solution:
def evalRPN(self, tokens: List[str]) -> int:
stack_result = []
for i in tokens:
if i not in '+-*/':
stack_result.append(int(i))
else:
num1 = stack_result.pop()
num2 = stack_result.pop()
result = self.do_math(i,num2,num1)
stack_result.append(result)
return int(stack_result.pop())
def do_math(self,method,x,y):
if method=='+':
return x+y
elif method=='-':
return x-y
elif method=='*':
return x*y
else:
if x<0 and y<0:
return x//y
elif x<0 or y<0:
return (math.ceil(x/y))
else:
return x//y