digits = [9,9,9]
^
First Pass: i = 2
if digits[i] != 9 --> false
if i(2) > 0 and digits[i] == 9 --> true
digits[i] = 0
else <nothing>
digits = [9,9, *0*]
----
[9,9,0]
^
Second Pass: i = 1
if digits[i] != 9 --> false
if i(1) > 0 and digits[i] == 9 --> true
digits[i] = 0
else <nothing>
digits = [9,*0*,0]
-----
[9,0,0]
^
Third Pass: i = 0
if digits[i] != 9 --> false
if i(0) > 0 and digits[i] == 9 --> true
else
digits[i] = 1
digits = append(digits, 0)
digits = [*1*,0,0,*0*]
Last active
January 2, 2026 01:25
-
-
Save tdadadavid/a2de815522d4b74feb02115caf5de392 to your computer and use it in GitHub Desktop.
leetcode day 1
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
| func plusOne(digits []int) []int { | |
| // access array in reverse order. | |
| for i := len(digits)-1; i >= 0; i-- { | |
| if digits[i] != 9 { | |
| digits[i] = digits[i]+1 | |
| return digits | |
| } else if i > 0 && digits[i] == 9 { | |
| digits[i] = 0 | |
| } else { | |
| digits[i] = 1 | |
| digits = append(digits, 0) // this particular line helps the array grow when neccessary | |
| } | |
| } | |
| return digits | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment