public class Solution {
public int[] plusOne(int[] digits) {
boolean hasCarry = false;
for(int i = digits.length - 1; i >= 0; i--) {
digits[i]++;
if(digits[i] == 10) {
digits[i] = 0;
if(i == 0) {
hasCarry = true;
}
} else {
break;
}
}
if(hasCarry) {
int[] newDigits = new int[digits.length + 1];
newDigits[0] = 1;
System.arraycopy(digits, 0, newDigits, 1, digits.length);
return newDigits;
}
return digits;
}
}
Time: O(n)
Space: O(n)