Link: https://leetcode.com/problems/basic-calculator/
Solution:
Topics: stack
Intuition
This problem is riddled with edge cases, but overall it is not that difficult. The basic idea is to use a stack to evaluate the expression. Essentially, whenever a )
is seen, we can evaluate everything between the closed bracket and the next open bracket on the stack.
There is one catch however. Addition is associative (agnostic to grouping) and commutative (agnostic to order), but subtraction is neither so we must evaluate the expression from left to right to get the correct result.
The problem is, if we pop the expression off the stack, and append it to a list for evaluation, this list expression will be in reverse order…so the idea is to either use a queue for efficient front insertions or just reverse the expression before evaluating.
There are a few more edge cases to consider:
-
can be unary so when it is, append a0
to the front.- numbers can be greater than 9, so build multi-digit numbers accordingly.
- handle empty spaces.
Implementation
Review 1
Super annoying problem! Remember to feed the evaluate function a list (or deque) and if the first element is a -
, then append a 0
to the front.