Created
March 5, 2022 11:12
-
-
Save esase/be06d0ed345afc6ae31a35c9f86b0f7a to your computer and use it in GitHub Desktop.
Plus one [https://leetcode.com/problems/plus-one]
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
/** | |
* @param {number[]} digits | |
* @return {number[]} | |
*/ | |
var plusOne = function(digits) { | |
let index = digits.length - 1; | |
while (index >= 0) { | |
if (digits[index] + 1 <= 9) { | |
digits[index] = digits[index] + 1; | |
break; | |
} | |
digits[index] = 0; | |
index--; | |
} | |
if (digits[0] === 0) { | |
digits[0] = 1; | |
digits.push(0); | |
} | |
return digits; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment