classSolution: defcalculate(self, s: str) -> int: idx, stack = 0, [] res = 0# 当前的数字 sign = '+'# 前一个符号 defupdate(op, v): if op == "+": stack.append(v) if op == "-": stack.append(-v) if op == "*": stack.append(stack.pop() * v) #for BC II and BC III if op == "/": stack.append(int(stack.pop() / v)) #for BC II and BC III while idx < len(s): if s[idx].isdigit(): res = res*10+int(s[idx]) print(res) elif s[idx] in'+-*/': update(sign, res) print(res, sign) res, sign = 0, s[idx] print(res, sign) print(stack) idx += 1 # 这里不要忘记,因为还有最后一个符号没有处理 update(sign, res) print(stack)
elif s[it] == "(": # For BC I and BC III num, j = self.calculate(s[it + 1:]) it = it + j elif s[it] == ")": # For BC I and BC III update(sign, num) returnsum(stack), it + 1