Skip to content

Instantly share code, notes, and snippets.

@Cee
Last active August 29, 2015 14:01
Show Gist options
  • Save Cee/fa5d79908932da66b697 to your computer and use it in GitHub Desktop.
Save Cee/fa5d79908932da66b697 to your computer and use it in GitHub Desktop.
public class Solution {
public int[] plusOne(int[] digits) {
int length = digits.length;
int plus = 1;
for (int i = length - 1; i >= 0; i--){
digits[i] += plus;
if (digits[i] >= 10){
plus = 1;
digits[i] -= 10;
} else {
plus = 0;
}
}
if (plus == 0){
return digits;
}else{
int[] ret = new int[length + 1];
ret[0] = 1;
for (int i = 1; i < length + 1; i++){
ret[i] = digits[i - 1];
}
return ret;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment