leetcode-study

class Solution:
    def calculate(self, s: str) -> int:
        """
        Evaluate a basic mathematical expression containing integers, '+', '-', parentheses, and spaces.
        
        Parameters:
            s (str): A valid string expression to be evaluated.
        
        Returns:
            int: The result of the evaluated expression.
        """
        stack = []      # Stack to keep track of previous results and signs.
        result = 0      # Holds the cumulative result of the expression.
        number = 0      # Holds the current number being constructed from digits.
        sign = 1        # Current sign multiplier; 1 for positive, -1 for negative.
        
        for char in s:
            if char.isdigit():
                # Construct the number from consecutive digits.
                number = number * 10 + int(char)
            elif char in ['+', '-']:
                # Update result with the current number and sign.
                result += sign * number
                # Set sign for the next number.
                sign = 1 if char == '+' else -1
                # Reset current number.
                number = 0
            elif char == '(':
                # Push the current result and sign onto the stack.
                stack.append(result)
                stack.append(sign)
                # Reset result and sign for the new sub-expression.
                result = 0
                sign = 1
            elif char == ')':
                # Finish the current number before closing parenthesis.
                result += sign * number
                number = 0  # Reset number after processing.
                # Pop sign and previous result from the stack.
                prev_sign = stack.pop()
                prev_result = stack.pop()
                # Combine the sub-expression result with the previous context.
                result = prev_result + prev_sign * result
            # Ignore any whitespace characters.
        
        # Add any number left after the loop.
        result += sign * number
        return result

Summary of Techniques and Approaches: