Problem Description
Implement a basic calculator to evaluate a simple expression string. The expression contains only non-negative integers, the operators '+', '-', '*', '/', and parentheses '(' and ')'. The expression is always valid, and integer division should truncate toward zero.
Key Insights
- Use recursion to handle nested expressions defined by parentheses.
- A stack can be used to correctly evaluate the expression and respect operator precedence.
- Multiplication and division have higher precedence than addition and subtraction.
- When encountering an open parenthesis, recursively compute its value until a closing parenthesis is found.
- Use immediate calculation for multiplication and division by updating the top of the stack before moving to the next operator.
Space and Time Complexity
Time Complexity: O(n) where n is the length of the string, as every character is processed. Space Complexity: O(n) due to the recursion stack or auxiliary stack used for intermediate results.
Solution
We use a recursive approach combined with a stack to solve the problem. As we iterate through the string:
- Convert digits into numbers.
- When encountering an operator or parenthesis, process the previously accumulated number based on the preceding operator.
- For multiplication or division, pop the last number from the stack, apply the operation, and push the result back.
- When a '(' is encountered, recursively evaluate the substring until the corresponding ')'.
- After processing all tokens, sum the values in the stack to get the final result. This method correctly handles both operator precedence and nested expressions.
Code Solutions
Below are code solutions in Python, JavaScript, C++, and Java with line-by-line comments.