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 carry = 1;
for(int i=digits.length-1;i>=0;i--)
{
int new_carry = digits[i] + carry;
digits[i] = new_carry % 10;
carry = new_carry / 10;
}
if(carry>0)
{
int[] array = new int[digits.length+1];
array[0] = carry;
for(int i=1;i<=digits.length;i++)
{
array[i] = digits[i-1];
}
return array;
}
else
return digits;
}
}