Created
July 11, 2021 22:57
-
-
Save wohhie/8d5e26510b7f00d4df64b8bd4de0e0db to your computer and use it in GitHub Desktop.
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
public int[] plusOne(int[] digits) { | |
// digits = [1, 2, 3] // output: [1, 2, 4] | |
// digits = [9, 9, 9] // output: [1, 0, 0, 0] | |
for(int idx = digits.length - 1; idx >= 0; idx--){ | |
if(digits[idx] < 9){ | |
digits[idx] += 1; | |
return digits; | |
} | |
digits[idx] = 0; | |
} | |
int[] results = new int[digits.length + 1]; | |
results[0] = 1; | |
return results; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment