java 0 ms solution


  • 0
    J

    public static int[] plusOne(int[] digits){
    int carry=0,i=digits.length-1;
    int sum=digits[i]+1+carry;
    carry=sum>9?1:0;
    digits[i]=sum%10;
    while(carry>0&&i>0){
    i--;
    sum=digits[i]+carry;
    digits[i]=sum%10;
    carry=sum>9?1:0;
    }
    if(carry>0){
    int nums[]=new int[digits.length+1];
    System.arraycopy(digits, 0, nums, 1, digits.length);
    nums[0]=1;
    return nums;
    }
    return digits;
    }


  • 0
    X

    @jiangwenqisz - No need for the System.arraycopy. I believe at this point in your code, the digits array is all zeros.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.