Accepted consistent Java code.


  • 4
    P
    public int[] plusOne(int[] digits) {
        int i=digits.length;
        boolean one = true;
        while (--i >= 0 && one) {
        	digits[i]++;
        	one = digits[i] == 10;
        	if (one) digits[i] = 0;
        }
        if (one) {
        	digits = new int[digits.length+1];
        	digits[0] = 1;
        }
        return digits;
    }

  • 0
    L

    The part you define a new array with the first 1 and the rest are "0" is smart!
    digits = new int[digits.length+1];
    digits[0] = 1;


  • 3
    M

    Your method changes the input. I'm not sure if that's a good decision.


  • 0
    P

    It changes when no space left in origin array. That's ok.


Log in to reply
 

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