Basic Calculator II LeetCode solution in Python
1# Runtime: 201 ms (Top 18.42%) | Memory: 15.8 MB (Top 36.40%)2class Solution:3def calculate(self, s: str) -> int:4stack = []5currentNumber = 06operator = "+"7operations = "+-/*"8for i in range(len(s)):9ch = s[i]10if ch.isdigit():11currentNumber = currentNumber * 10 + int(ch)1213if ch in operations or i == len(s) - 1:14if operator == "+":15stack.append(currentNumber)1617elif operator == "-":18stack.append(-currentNumber)1920elif operator == "*":21stack.append(stack.pop() * currentNumber)2223elif operator == "/":24stack.append(int(stack.pop() / currentNumber))2526currentNumber = 027operator = ch2829return sum(stack)