Tensorum
YouTubeশেখা শুরু করুন
LeetCode 66 · Other

Plus One

LeetCode 66 · সহজ · সমাধান, intuition ও ধাপে ধাপে ওয়াকথ্রু

Carry propagation from the right.

সহজArrayMath
LeetCode-এ খুলুন ↗

আমি 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].