Last active
August 29, 2015 14:01
-
-
Save Cee/fa5d79908932da66b697 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 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