Skip to content

Instantly share code, notes, and snippets.

@ugandapinik
Created July 11, 2021 22:57
Show Gist options
  • Save ugandapinik/8d5e26510b7f00d4df64b8bd4de0e0db to your computer and use it in GitHub Desktop.
Save ugandapinik/8d5e26510b7f00d4df64b8bd4de0e0db to your computer and use it in GitHub Desktop.
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