Javascript solution O(n) time and O(1) space


  • 0
    R
    var plusOne = function(digits) {
        var residue = 1,
            pointer = digits.length - 1;
        
        while(pointer >= 0){
            if(residue === 1){
                if(digits[pointer] === 9){
                    digits[pointer] = 0;
                    residue = 1;
                }else{
                    digits[pointer] += 1;
                    residue = 0;
                }
            }
            pointer--;
        }
        
        if(residue === 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.