Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 224 · Expression Evaluation

Basic Calculator

LeetCode 224 · கடினம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

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

கடினம்MathStringStackRecursion
LeetCode இல் திற ↗

நான் Basic Calculator (LeetCode 224) ஐ இப்படித் தீர்த்தேன் — Expression Evaluation பேட்டர்னின் ஒரு கடினம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் 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.

நான் எப்படித் தீர்த்தேன்

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
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Basic Calculator — அடிக்கடி கேட்கப்படும் கேள்விகள்

Basic Calculator பின்னாலுள்ள 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.

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.

Basic Calculator இன் time மற்றும் space complexity என்ன?

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

Basic Calculator இல் பொதுவான தவறுகள் என்ன?

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