LeetCode – Plus One (Java)

Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list.

Java Solution

To solve this problem, we can use a flag to mark if the current digit needs to be changed.

public int[] plusOne(int[] digits) {
    if(digits==null||digits.length==0)
        return new int[0];
 
    int carry = 1;    
    for(int i=digits.length-1; i>=0; i--){
        int sum = digits[i]+carry;
        if(sum>=10){
            carry=1;
        }else{
            carry=0;
        }
        digits[i]=sum%10;
    }
 
    if(carry==1){
        int[] result = new int[digits.length+1];
        System.arraycopy(digits, 0, result, 1, digits.length);
        result[0]=1;
        return result;
    }else{
        //int[] result = new int[digits.length];
        return digits;
    }
}

5 thoughts on “LeetCode – Plus One (Java)”


  1. public int[] plusOne(int[] digits) {
    int[] result = new int[digits.length];
    boolean flag = true;
    for(int v : digits){
    if(v!=9){
    flag = false;
    break;
    }
    }
    if(flag){
    result = new int[digits.length+1];
    result[0] = 1;
    }

    int carry = 0;
    int num = 0;
    for(int i = digits.length -1; i >= 0;i--){
    if(i == (digits.length -1)){
    num = digits[i] + 1;
    }else{
    num = digits[i] + carry;
    }
    if(num >= 10){
    num = num - 10;
    carry = 1;
    }else{
    carry = 0;
    }
    if(flag){
    result[i+1] =num;
    }else{
    result[i] =num;
    }
    }
    return result;
    }

  2. Optimized the code to prevent additional looping

    public static int[] plusOne(int[] digits) {
    int len = digits.length;
    for (int i = len – 1; i >= 0; i–) {
    if (digits[i] == 9) {
    digits[i] = 0;
    } else {
    digits[i] = digits[i] + 1;
    break;
    }

    if (i == 0 && digits[i] == 0) {
    int[] y = new int[len + 1];
    y[0] = 1;
    for (int j = 1; j <= len; j++) {
    y[j] = digits[j – 1];
    }
    digits = y;
    }
    }

    return digits;
    }

  3. public static int[] plusOne(int[] digits) {

    int length = digits.length;

    boolean flag = false;

    for (int i = length – 1; i >= 0; i–) {

    if (digits[i] == 9) {

    flag = true;

    digits[i] = 0;

    } else if (flag) {

    digits[i] = digits[i] + 1;

    flag = false;

    }

    }

    if (flag) {

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

    newArray[0] = 1;

    System.arraycopy(digits, 0, newArray, 1, digits.length);

    return newArray;

    }

    return digits;

    }

  4. Slightly more readable version

    public int[] plusOne(int[] digits) {
    int len = digits.length;

    for (int i = len – 1; i >= 0; –i) {
    if (digits[i] == 9) {
    digits[i] = 0;
    } else {
    ++digits[i];
    return digits;
    }
    }
    //we have to add a digit at the head
    int[] y = new int[len + 1];
    y[0] = 1;
    for (int j = 1; j <= len; ++j) {
    y[j] = digits[j – 1];
    }
    return y;
    }

Leave a Comment