0ms Java solution


  • 2
    V

    public class Solution {
    public int[] plusOne(int[] digits) {

        for(int i = digits.length - 1; i >= 0; i--) {
            if(digits[i] < 9) {
                digits[i]++;
                return digits;
            }
            else {
                digits[i] = 0;
                if(i == 0) {
                    int[] newDigits = new int[digits.length+1];
                    newDigits[0] = 1;
                    System.arraycopy(digits, 0, newDigits, 1, digits.length);
                    return newDigits;
                }
            }
        }
        return digits;
    }
    

    }


  • 0
    F

    Do you need copy? I think they are all 0 from the second to end
    System.arraycopy(digits, 0, newDigits, 1, digits.length);


Log in to reply
 

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