KatInterview

66. Plus One

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to take an array of digits representing a non-negative integer and add one to it. The digits are arranged from most significant to least significant. For example, [1, 2, 3] represents the number 123. The core of the problem lies in correctly handling the carry-over logic, similar to how we do addition by hand. The most interesting case is when the addition results in an increase in the number of digits, such as incrementing 99 to 100.

Solution

Plus One in Python

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

        # Iterate from the last digit to the first
        for i in range(n - 1, -1, -1):
            # If the current digit is less than 9, just increment it and return
            if digits[i] < 9:
                digits[i] += 1
                return digits
            # If the current digit is 9, it becomes 0, and we carry over to the next digit
            digits[i] = 0

        # If the loop completes, it means all digits were 9s (e.g., [9,9] -> [0,0]).
        # We need to add a new leading 1.
        # e.g. [0, 0, 0] becomes [1, 0, 0, 0]
        return [1] + digits

Plus One in Java

class Solution {
    public int[] plusOne(int[] digits) {
        int n = digits.length;

        // Iterate from the last digit to the first
        for (int i = n - 1; i >= 0; i--) {
            // If the current digit is less than 9, just increment it and return
            if (digits[i] < 9) {
                digits[i]++;
                return digits;
            }
            // If the current digit is 9, it becomes 0, and we carry over to the next digit
            digits[i] = 0;
        }

        // If the loop completes, it means all digits were 9s (e.g., [9,9] -> [0,0]).
        // We need to create a new array with one more digit.
        int[] newNumber = new int[n + 1];
        newNumber[0] = 1; // The new leading digit is 1, others are 0 by default
        return newNumber;
    }
}

Plus One in C++

class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        int n = digits.size();

        // Iterate from the last digit to the first
        for (int i = n - 1; i >= 0; --i) {
            // If the current digit is less than 9, just increment it and return
            if (digits[i] < 9) {
                digits[i]++;
                return digits;
            }
            // If the current digit is 9, it becomes 0, and we carry over to the next digit
            digits[i] = 0;
        }

        // If the loop completes, it means all digits were 9s (e.g., [9,9] -> [0,0]).
        // We need to add a new leading 1.
        // Create a new vector of size n+1, initialized to 0s.
        vector<int> newNumber(n + 1, 0);
        newNumber[0] = 1;
        return newNumber;
    }
};

Plus One in TypeScript

function plusOne(digits: number[]): number[] {
    const n = digits.length;

    // Iterate from the last digit to the first
    for (let i = n - 1; i >= 0; i--) {
        // If the current digit is less than 9, just increment it and return
        if (digits[i] < 9) {
            digits[i]++;
            return digits;
        }
        // If the current digit is 9, it becomes 0, and we carry over to the next digit
        digits[i] = 0;
    }

    // If the loop completes, it means all digits were 9s (e.g., [9,9] -> [0,0]).
    // We need to add a new leading 1.
    // Using the spread operator creates a new array with 1 at the beginning.
    return [1, ...digits];
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.