Plus One 2021-05-30 20:00

Problem Description

public static int[] plusOne(int[] digits) {
    int lastIndex = digits.length - 1;
    int adder = 1;
    for (int i = lastIndex; i >= 0; i--) {
        digits[i] = digits[i] + adder;
        if (digits[i] == 10) {
            digits[i]=0;
            adder=1;
        }else{
            adder=0;
        }
    }
    if(adder==1){
        int[] res = new int[lastIndex+2];
        res[0]=1;
        return res;
    }
    return digits;
}
Runtime Memory
0 ms 36.9 MB

henryxi leetcode list

EOF