Javascript, solution-sharing


  • 1
    C
    /**
     * note: [9] plus one [1, 0] // digits[0]=10
     */
    var plusOne = function(digits) {
        var carry = 1;
        for(var i=digits.length-1; i>=0; i--){
            digits[i] = digits[i] + carry;
            digits[i] = (digits[i]>=10)?(carry=1, digits[i]-10):(carry=0, digits[i]);
        }
        if(carry){ digits.unshift(1); } // note
        return digits;
    };

Log in to reply
 

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