Tensorum
YouTubeसीखना शुरू करें
LeetCode 66 · Other

Plus One

LeetCode 66 · आसान · समाधान, intuition और कदम-दर-कदम वॉकथ्रू

Carry propagation from the right.

आसानArrayMath

मैंने Plus One (LeetCode 66) ऐसे हल किया — Other पैटर्न का एक आसान प्रॉब्लम। नीचे: intuition, कदम-दर-कदम approach, मेरा accepted Python कोड, animated test cases और line-by-line debugger।

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

Intuition

Add one to the last digit and propagate the carry leftwards. If the carry survives past the first digit (all nines), prepend a 1.

Approach

  1. 1

    Add 1 to the last digit.

  2. 2

    While there is a carry, add it to the next digit left.

  3. 3

    Leftover carry → prepend it.

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

Carry loopPythonO(n) time
class Solution:
    def plusOne(self, digits: List[int]) -> List[int]:
        carry=0
        n = len(digits)
        last_digit=digits[n-1]
        sum = last_digit+1
        carry = sum//10
        digits[n-1]=sum%10
        r=n-2
        while carry!=0 and r>=0:
            sum = digits[r]+carry
            carry = sum//10
            digits[r]=sum%10
            r-=1
        if carry>0:
            digits=[carry]+digits
        return digits
स्टेप-थ्रू लोड हो रहा है…

Plus One — अक्सर पूछे जाने वाले प्रश्न

Plus One के पीछे intuition क्या है?

Add one to the last digit and propagate the carry leftwards. If the carry survives past the first digit (all nines), prepend a 1.

Plus One (LeetCode 66) कैसे हल करें?

1. Add 1 to the last digit. 2. While there is a carry, add it to the next digit left. 3. Leftover carry → prepend it.

Plus One की time और space complexity क्या है?

Time: O(n)। Space: O(1) (O(n) when growing)। (Accepted approach: Carry loop।)

Plus One में आम गलतियाँ क्या हैं?

[9,9,9] becomes [1,0,0,0].