LeetCode #66 Plus One Solution & Explanation

LeetCode Problem

You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0‘s.

Increment the large integer by one and return the resulting array of digits.


Solution

In this problem, we need to iterate through the array in reverse order.

For each digit, we increment it by one, and if the sum is equal to ten, it means we need to proceed to the next iteration.

If not, we can return the resulting array.

If we have iterated through the entire array and still need to carry over, we add an additional element with a value of 1 at the beginning of the original array.

C# Solution

Solution1

public class Solution {
    public int[] PlusOne(int[] digits) {

        int len =  digits.Length;
        int num;
        int add = 1;

        for(int i = len-1;i>=0;i--)
        {
            num = digits[i]+add;
            if(num == 10)
            {
                digits[i] = 0;
            }
            else
            {
                digits[i] = num;
                return digits;
            }
        }

        int[] newDigits = new int[digits.Length + 1];

        newDigits[0] = 1;
        Array.Copy(digits, 0, newDigits, 1, digits.Length);

        return newDigits;

    }
}

Java Solution

Solution1

class Solution {
    public int[] plusOne(int[] digits) {
        int len =  digits.length;
        int num;
        int add = 1;

        for(int i = len-1;i>=0;i--)
        {
            num = digits[i]+add;
            if(num == 10)
            {
                digits[i] = 0;
            }
            else
            {
                digits[i] = num;
                return digits;
            }
        }

        int[] newDigits = new int[digits.length + 1];

        newDigits[0] = 1;
        System.arraycopy(digits, 0, newDigits, 1, digits.length);

        return newDigits;
    }
}

Python3 Solution

Solution1

class Solution:
    def plusOne(self, digits: List[int]) -> List[int]:
        add = 1

        for i in range(len(digits)-1,-1,-1):
            num = digits[i]+add
            if(num == 10):
                digits[i] = 0
            else:
                digits[i] = num
                return digits
        
        digits.insert(0,1)
        return digits

JavaScript Solution

Solution1

/**
 * @param {number[]} digits
 * @return {number[]}
 */
var plusOne = function(digits) {
        var len =  digits.length;
        var num;
        var add = 1;

        for(let i = len-1;i>=0;i--)
        {
            num = digits[i]+add;
            if(num == 10)
            {
                digits[i] = 0;
            }
            else
            {
                digits[i] = num;
                return digits;
            }
        }

        digits.unshift(1);
        return digits;
};

In JavaScript, we have introduced how to add an element at the front of an array.

See here ➡ Manipulating the First Element of an Array in JavaScript: shift() and unshift()


Conclusion

With a little care, we can easily solve this problem with an iteration.

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts, thanks a lot

If you got any problem about the explanation, please feel free to let me know

The problem link : Plus One – LeetCode

Some Random LeetCode Post

Leave a Reply

Your email address will not be published. Required fields are marked *