class Solution: def calculate(self, s: str) -> int: stack = [] num = 0 sign = 1 result = 0 for char in s: if char.isdigit(): num = num * 10 + int(char) elif char == "+": result += sign * num num = 0 sign = 1 elif char == "-": result += sign * num num = 0 sign = -1 elif char == "(": stack.append(result) stack.append(sign) result = 0 sign = 1 elif char == ")": result += sign * num num = 0 result *= stack.pop() # Pop the sign. result += stack.pop() # Pop the preceding result. result += sign * num return result # Example usage: solution = Solution() s = "(1+(4+5+2)-3)+(6+8)" result = solution.calculate(s) print(result) # Output: 23