Simple Javascript solution


  • 0
    L
    var plusOne = function(digits) {
        let carry = 1;
        var i = digits.length-1;
        while(i >= 0 && carry === 1) {
            digits[i] = digits[i] + carry;
            if(digits[i] === 10) {
                digits[i] = 0;
                i--;
            }else if(digits[i] < 10) {
                carry = 0;
            }
        } 
        if(carry === 1) {
            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.