Skip to content

Instantly share code, notes, and snippets.

@esase
Created March 5, 2022 11:12
Show Gist options
  • Save esase/be06d0ed345afc6ae31a35c9f86b0f7a to your computer and use it in GitHub Desktop.
Save esase/be06d0ed345afc6ae31a35c9f86b0f7a to your computer and use it in GitHub Desktop.
/**
* @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