Tensorum
YouTubeसीखना शुरू करें
LeetCode 282 · Constraint Satisfaction

Expression Add Operators

LeetCode 282 · कठिन · समाधान, intuition और कदम-दर-कदम वॉकथ्रू

Track prev_operand to fix up multiplication precedence.

कठिनMathStringBacktracking

मैंने Expression Add Operators (LeetCode 282) ऐसे हल किया — Constraint Satisfaction पैटर्न का एक कठिन प्रॉब्लम। नीचे: intuition, कदम-दर-कदम approach, मेरा accepted Python कोड, animated test cases और line-by-line debugger।

नीचे का विवरण अंग्रेज़ी में है।

Intuition

Try every way to cut the digit string into numbers and put +, − or * between them. Multiplication binds tighter, so remember the last operand: to apply *, undo it from the running value and add (last × current) instead.

Approach

  1. 1

    dfs(index, value, prev, expr).

  2. 2

    Try each next number num[index..i] (no leading zeros).

  3. 3

    First number starts the expression.

  4. 4

    +: value + cur; −: value − cur (prev = −cur); *: value − prev + prev·cur.

मैंने कैसे हल किया

Backtracking with precedence fixPythonO(4ⁿ) time
class Solution:
    def addOperators(self, num: str, target: int) -> List[str]:
        res = []
        def dfs(index,cur_val,prev_operand,expression):
            if index==len(num):
                if cur_val==target:
                    res.append(expression)
                return
            for i in range(index,len(num)):
                if i!=index and num[index]=="0":
                    continue
                cur_str = num[index:i+1]
                cur_num = int(cur_str)
                if index==0:
                    dfs(i+1,cur_num,cur_num,cur_str)
                else:
                    #+
                    dfs(i+1,cur_val+cur_num,cur_num,expression+"+"+cur_str)
                    #-
                    dfs(i+1,cur_val-cur_num,-cur_num,expression+"-"+cur_str)
                    #*
                    #val-prevoperand+(prevoperand*cur_num)
                    dfs(i+1,cur_val-prev_operand+(prev_operand*cur_num),prev_operand * cur_num,expression+"*"+cur_str)
                    #(2,3*2,3-2+3*2, 1+2*3
        dfs(0,0,0,"")
        return res


            


                
स्टेप-थ्रू लोड हो रहा है…

Expression Add Operators — अक्सर पूछे जाने वाले प्रश्न

Expression Add Operators के पीछे intuition क्या है?

Try every way to cut the digit string into numbers and put +, − or * between them. Multiplication binds tighter, so remember the last operand: to apply *, undo it from the running value and add (last × current) instead.

Expression Add Operators (LeetCode 282) कैसे हल करें?

1. dfs(index, value, prev, expr). 2. Try each next number num[index..i] (no leading zeros). 3. First number starts the expression. 4. +: value + cur; −: value − cur (prev = −cur); *: value − prev + prev·cur.

Expression Add Operators की time और space complexity क्या है?

Time: O(4ⁿ)। Space: O(n)। (Accepted approach: Backtracking with precedence fix।)

Expression Add Operators में आम गलतियाँ क्या हैं?

Numbers like '05' are invalid — skip multi-digit numbers starting with 0.