/javascript

Plus One

The way i solved.

/**
 * @param {number[]} digits
 * @return {number[]}
 */
var plusOne = function(digits) {
    let last = digits.length - 1
    while (last >= 0) {
        if (digits[last] === 9) {
            digits[last] = 0
            last--
        } else {
            digits[last] += 1
            break
        }
    }
    if (last <  0) {
        digits.unshift(1)
    }
    return digits
};

which is about 84ms

Subscribe to Necmettin Karakaya

Get the latest posts delivered right to your inbox

Necmettin Karakaya

Necmettin Karakaya

Full Stack Developer