My JAVA solution, it's neat but why cause it so slow?


  • 0
    J
    public class Solution {
    public int[] plusOne(int[] digits) {
        int extra = 0;
        int index = digits.length - 1;
        int[] result = new int[digits.length];
        while (index >= 0 && digits[index] == 9) {
            digits[index] = 0;
            index--;
        }
        if (index == -1) {
            result = new int[digits.length+1];
            result[0] = 1;
        }else {
            digits[index] += 1;
            return digits;
        }
        return result;
    }
    

    }


  • 0
    N

    You are re creating the array as a new object when the leftmost digit becomes 9+1 and thus creating a new array is a laborius task


Log in to reply
 

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