Simple Java Solution


  • 0
    H

    //use one pointer to point to the digit not equal to 9.
    public int[] plusOne(int[] digits) {
    int non9 = -1;
    for (int i = digits.length - 1; i >= 0; i++){
    if (digits[i] != 9) {
    non9 = i;
    break;
    }
    }
    if (non9 == -1) {
    int[] result = new int[digits.length + 1];
    result[0] = 1;
    return result;
    }
    else {
    digits[non9] = digits[non9] + 1;
    for (int i = non9 + 1; i < digits.length; i++) {
    digits[i] = 0;
    }
    return digits;
    }

    }

Log in to reply
 

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