Given a non-negative number represented as an array of digits, plus one to the number.
The digits are stored such that the most significant digit is at the head of the list.
public class Solution {
public int[] plusOne(int[] digits) {
int flag = 0;
if(digits == null) return null;
int len = digits.length;
if(len == 0) return digits;
digits[len-1] += 1;
for(int i=len-1; i>=0; i--){
int t = digits[i] + flag;
digits[i] = t % 10;
flag = t / 10;
if(flag == 0) break;
}
if(flag == 0) return digits;
int nDigits[] = new int[len + 1];
nDigits[0] = 1;
for(int i=0; i<len; i++){
nDigits[i+1] = digits[i];
}
return nDigits;
}
}