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.
题意:将一个用一维数组表组的数加一,同样得到一个数组。
代码:要考虑到最高位,即数组下标0的值
public class Solution {
public int[] plusOne(int[] digits) {
int length = digits.length;
int[] result = new int[length + 1];
int tmp = 0, add = 1;
for(int i = length - 1; i >=0 ; i--)
{
result[i + 1] = (digits[i] + add + tmp) % 10;
tmp = (digits[i] + add + tmp) / 10;
add = 0;
}
if(tmp > 0)
{
result[0] = tmp;
return result;
}
else
{
for(int i = 0; i < length; i++)
digits[i] = result[i + 1];
return digits;
}
}
}