Tensorum
LeetCode 224 · Expression Evaluation

Basic Calculator

LeetCode 224 · Hard · solution, intuition & step-by-step walkthrough

Stack saves (answer, sign) at each '('.

HardMathStringStackRecursion
76 msruntime · beats 17%
20.4 MBmemory · beats 26%
1approach
5accepted runs · last 9 Mar 2026

This is how I solved Basic Calculator (LeetCode 224), a hard problem from the Expression Evaluation pattern. Below: the intuition, the approach step by step, my accepted Python code, animated test cases and a line-by-line debugger.

Intuition

Without * or /, an expression is just signed numbers added up. Keep a running answer and the current sign. On '(' save the answer and sign so far on a stack and start fresh; on ')' finish the inner value, apply the saved sign, and add the saved answer.

Approach

  1. 1

    Digits → read the full number, add sign × number.

  2. 2

    '+' / '−' set the sign.

  3. 3

    '(' → push ans and sign, reset.

  4. 4

    ')' → ans = saved_ans + saved_sign × ans.

How I solved it

Sign stackPythonO(n) time
class Solution:
    def calculate(self, s: str) -> int:
        #if it is a number
        #calculate number and add to answer with the current sign
        #if it's ( - add current answer and sign to the stack
        #if it's ) - take last sign, last total and apply to the ans
        #if it's - - sign =-1
        #if it's +  sign=1
        i=0
        ans = 0
        sign = 1
        stack = []
        while i<len(s):
            if s[i].isdigit(): 
                num = 0
                #"123"
                while i<len(s) and s[i].isdigit():
                    num = num*10+int(s[i])
                    i+=1
                ans+=num*sign
                i-=1 #we incremented extra
            elif s[i]=="+":
                sign = 1
            elif s[i]=="-":
                sign =-1
            elif s[i]=="(":
                stack.append(ans)
                stack.append(sign)
                #reset
                ans=0
                sign = 1
            elif s[i]==")":
                last_sign = stack.pop()
                last_total=stack.pop()
                ans = ans*last_sign
                ans+=last_total
            i+=1
        return ans
Loading the step-through…

Basic Calculator — FAQ

What is the intuition behind Basic Calculator?

Without * or /, an expression is just signed numbers added up. Keep a running answer and the current sign. On '(' save the answer and sign so far on a stack and start fresh; on ')' finish the inner value, apply the saved sign, and add the saved answer.

How do you solve Basic Calculator (LeetCode 224)?

1. Digits → read the full number, add sign × number. 2. '+' / '−' set the sign. 3. '(' → push ans and sign, reset. 4. ')' → ans = saved_ans + saved_sign × ans.

What is the time and space complexity of Basic Calculator?

Time: O(n). Space: O(n). (Accepted approach: Sign stack.)

What are common mistakes in Basic Calculator?

Step back one index after reading a multi-digit number.