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.