JS solution


  • 0
    var plusOne = function(digits) {
        const length = digits.length;
        let i = length - 1;
        digits[i]++;
        while (i > 0 && digits[i] > 9) {
            digits[i] = 0;
            digits[i - 1]++;
            i--;        
        }
        if (i === 0 && digits[0] > 9) {
            digits[0] = 0;
            digits.unshift(1);
        }
        return digits;
    };
    

Log in to reply
 

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