Created
July 30, 2023 03:24
-
-
Save CristianLlanos/97a90a5c9273ba8f2c2c4592ae5f1623 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function plusOne(digits: number[]): number[] { | |
let carry = 1; | |
for (let i = digits.length - 1; i >= 0; i--) { | |
if (carry === 0) { | |
return digits; | |
} | |
const sum = digits[i] + carry; | |
if (sum === 10) { | |
carry = 1; | |
digits[i] = 0; | |
} else { | |
carry = 0; | |
digits[i] = sum; | |
} | |
} | |
if (carry > 0) { | |
digits.unshift(1); | |
} | |
return digits; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment