Given a non-negative number represented as an array of digits, plus N to the number.
The digits are stored such that the most significant digit is at the head of the list.
N is guaranteed to be non-negative.
Solution:
public class Solution {
public int[] plusN(int[] digits, int n) {
int carry = n;
int index = digits.length-1;
while (carry>0 && index>=0){
int val = digits[index]+carry;
carry = val/10;
val = val%10;
digits[index]=val;
index--;
}
int[] res;
if (index<0 && carry>0){
String cStr = Integer.toString(carry);
res = new int[cStr.length()+digits.length];
for (int i=0;i<cStr.length();i++)
res[i]=cStr.charAt(i)-'0';
for (int i=0;i<digits.length;i++)
res[i+cStr.length()]=digits[i];
} else res = digits;
return res;
}
}