66. Plus One


  • 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.

Example 1

Input: digits = [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Incrementing by one gives 123 + 1 = 124.
Thus, the result should be [1,2,4].

Example 2

Input: digits = [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.
Incrementing by one gives 4321 + 1 = 4322.
Thus, the result should be [4,3,2,2].

Example 3

Input: digits = [9]
Output: [1,0]
Explanation: The array represents the integer 9.
Incrementing by one gives 9 + 1 = 10.
Thus, the result should be [1,0].

Method 1

【O(n) time | O(1) space】
package Leetcode.Array;

/**
 * @author zhengxingxing
 * @date 2024/12/08
 */
public class PlusOne {
    public static int[] plusOne(int[] digits) {
        // Iterate from the least significant digit (rightmost) to the most significant (leftmost)
        for (int i = digits.length - 1; i >= 0; i--) {
            // If the current digit is less than 9, we can simply increment and return
            // This handles most cases where no carrying is needed
            if(digits[i] < 9){
                digits[i]++;
                return digits;
            }

            // If the current digit is 9, set it to 0 and continue to the next digit
            // This handles the carrying scenario
            digits[i] = 0;
        }

        // Special case: when all digits are 9 (e.g., [9,9,9])
        // We need to create a new array with an additional digit
        // The new array will have 1 at the most significant position
        int[] newDigits = new int[digits.length + 1];
        newDigits[0] = 1;
        return newDigits;
    }

    public static void main(String[] args) {
        // Test Case 1: Normal scenario - last digit less than 9
        // Input: [1,2,3] → Expected Output: [1,2,4]
        int[] test1 = {1, 2, 3};
        int[] result1 = plusOne(test1);
        System.out.println("Test 1 - Input: [1,2,3], Output: " + arrayToString(result1));

        // Test Case 2: Scenario with 9 at the end requiring carry
        // Input: [1,9,9] → Expected Output: [2,0,0]
        int[] test2 = {1, 9, 9};
        int[] result2 = plusOne(test2);
        System.out.println("Test 2 - Input: [1,9,9], Output: " + arrayToString(result2));

        // Test Case 3: All 9s scenario - requires creating a new array
        // Input: [9,9,9] → Expected Output: [1,0,0,0]
        int[] test3 = {9, 9, 9};
        int[] result3 = plusOne(test3);
        System.out.println("Test 3 - Input: [9,9,9], Output: " + arrayToString(result3));
    }

    private static String arrayToString(int[] arr) {
        StringBuilder sb = new StringBuilder("[");
        for (int i = 0; i < arr.length; i++) {
            sb.append(arr[i]);
            if (i < arr.length - 1) {
                sb.append(",");
            }
        }
        sb.append("]");
        return sb.toString();
    }
}




Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • 2379. Minimum Recolors to Get K Consecutive Black Blocks
  • 2471. Minimum Number of Operations to Sort a Binary Tree by Level
  • 1387. Sort Integers by The Power Value
  • 2090. K Radius Subarray Averages
  • 2545. Sort the Students by Their Kth Score