plusOne with JavaScript


  • 0
    N
    var plusOne = function(digits) {
        let carry = 1;
        
        for(let i = digits.length - 1; i >= 0; i -= 1) {
            if (carry + digits[i] > 9) {
                digits[i] = 0;
                carry = 1;
            } else {
                digits[i] += carry;
                i = -1;
                carry = 0;
            }
        }
        
        if(carry) {
            digits.splice(0, 0, carry);
        }
        
        return digits;
    };
    

Log in to reply
 

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