Created
January 1, 2026 18:25
-
-
Save tatsuyax25/36a244c2cd2694fc0eec2d39592d1699 to your computer and use it in GitHub Desktop.
You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain a
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
| /** | |
| * @param {number[]} digits | |
| * @return {number[]} | |
| */ | |
| var plusOne = function(digits) { | |
| // Start from the last digit and move left | |
| for (let i = digits.length - 1; i >= 0; i--) { | |
| // If the current digit is less than 9, we can safely increment it | |
| // and return immediately because no carry is needed. | |
| if (digits[i] < 9) { | |
| digits[i]++; // simple increment | |
| return digits; // done - no ripple effect | |
| } | |
| // If the digit is 9, it becomes 0 and we carry 1 to the next digit. | |
| digits[i] = 0; | |
| } | |
| // If we exit the loop, it means all digits were 9. | |
| // Example: [9,9,9] -> [0,0,0] at this point. | |
| // We need to add a new leading 1. | |
| digits.unshift(1); | |
| return digits; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment