题目
Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero.
解题思路
使用栈,遇到加减法,直接把数加到栈中,乘除法需要先进行计算,将计算的结果再加入栈中
代码
class Solution:
def calculate(self, s: str) -> int:
stack, cur, op = [], 0, '+'
for c in s + '+':
if c == " ":
continue
elif c.isdigit():
cur = (cur * 10) + int(c)
else:
if op == '-':
stack.append(-cur)
elif op == '+':
stack.append(cur)
elif op == '*':
stack.append(stack.pop() * cur)
elif op == '/':
stack.append(int(stack.pop() / cur))
cur, op = 0, c
return sum(stack)