Fundamentals 7 min read

LeetCode Problem 66 – Plus One: Description, Examples, Analysis, and Solutions in Java, C++, C, and Python

This article first recounts a humorous interview anecdote involving an ex‑girlfriend, then presents LeetCode problem 66 “Plus One”, detailing its description, examples, constraints, a step‑by‑step analysis, and complete implementations in Java, C++, C, and Python.

IT Services Circle
IT Services Circle
IT Services Circle
LeetCode Problem 66 – Plus One: Description, Examples, Analysis, and Solutions in Java, C++, C, and Python

Recently a netizen shared a story about an interview where the interviewer turned out to be his ex‑girlfriend, leading to an awkward questioning session and a blocked phone number after the interview.

--------------下面是今天的算法题--------------

Problem Description : Given a non‑empty array of digits representing a non‑negative integer, add one to the integer. The most significant digit is at the start of the array, and each element stores a single digit. The integer does not have leading zeros except for zero itself.

Example 1 : Input: digits = [1,2,3] → Output: [1,2,4] (represents 123 → 124).

Example 2 : Input: digits = [4,3,2,1] → Output: [4,3,2,2] (represents 4321 → 4322).

Constraints : 1 ≤ digits.length ≤ 100, 0 ≤ digits[i] ≤ 9.

Problem Analysis : The task is to add one to a number represented by an array. If a digit is not 9, simply increment it and return. If a digit is 9, set it to 0 and continue the carry to the next higher digit. If all digits are 9, create a new array with length+1, set the first element to 1, and the rest to 0.

Java Solution :

public int[] plusOne(int[] digits) {
    int length = digits.length;
    // Traverse from the end
    for (int i = length - 1; i >= 0; i--) {
        if (digits[i] != 9) {
            // If current digit is not 9, add 1 and return
            digits[i]++;
            return digits;
        } else {
            // If current digit is 9, set to 0 and continue
            digits[i] = 0;
        }
    }
    // All digits were 9, need an extra digit
    int[] tmp = new int[length + 1];
    tmp[0] = 1;
    return tmp;
}

C++ Solution :

vector<int> plusOne(vector<int>& digits) {
    int length = digits.size();
    // Traverse from the end
    for (int i = length - 1; i >= 0; i--) {
        if (digits[i] != 9) {
            // If current digit is not 9, add 1 and return
            digits[i]++;
            return digits;
        } else {
            // If current digit is 9, set to 0 and continue
            digits[i] = 0;
        }
    }
    // All digits were 9, need an extra digit
    vector<int> tmp(length + 1);
    tmp[0] = 1;
    return tmp;
}

C Solution :

int* plusOne(int* digits, int digitsSize, int* returnSize) {
    // Traverse from the end
    for (int i = digitsSize - 1; i >= 0; i--) {
        if (digits[i] != 9) {
            // If current digit is not 9, add 1 and return
            digits[i]++;
            *returnSize = digitsSize;
            return digits;
        } else {
            // If current digit is 9, set to 0 and continue
            digits[i] = 0;
        }
    }
    // All digits were 9, need an extra digit
    int* tmp = (int*)malloc((digitsSize + 1) * sizeof(int));
    memset(tmp, 0, (digitsSize + 1) * sizeof(int));
    tmp[0] = 1;
    *returnSize = digitsSize + 1;
    return tmp;
}

Python Solution :

def plusOne(self, digits: List[int]) -> List[int]:
    length = len(digits)
    # Traverse from the end
    for i in range(length - 1, -1, -1):
        if digits[i] != 9:
            # If current digit is not 9, add 1 and return
            digits[i] += 1
            return digits
        else:
            # If current digit is 9, set to 0 and continue
            digits[i] = 0
    # All digits were 9, need an extra digit
    tmp = [0] * (length + 1)
    tmp[0] = 1
    return tmp
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

algorithmArrayplus-one
IT Services Circle
Written by

IT Services Circle

Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.