Created
February 1, 2024 15:03
-
-
Save iamandrewluca/cb339b31d666b9d74537a8603792c872 to your computer and use it in GitHub Desktop.
This file contains 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) { | |
let index = digits.length - 1 | |
while (true) { | |
digits[index] += 1; | |
if (digits[index] > 9 && index === 0) { | |
digits[index] = 0 | |
digits.unshift(1); | |
break; | |
} | |
if (digits[index] > 9) { | |
digits[index] = 0 | |
index--; | |
} else { | |
break; | |
} | |
} | |
return digits | |
} | |
plusOne([1,1,9]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment