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.
解题思路:
(1)重新分配一个数组,大小为digitsSize+1
(2)然后从后往前一次给新数组赋值
(3)最后判断新数组的newDigits[0]是不是为1;若为1就返回新数组,否则将数组头指向下一个单元返回
int* plusOne(int* digits, int digitsSize, int* returnSize) {
int* newDigits = (int*)malloc(sizeof(int)*(digitsSize+1));
int flags = 1;
for (int i = digitsSize-1; i >= 0; i--)
{
int temp = digits[i]+flags;
flags = temp/10;
newDigits[i+1] = temp%10;
}
if (flags == 1)
{
newDigits[0] = 1;
*returnSize = digitsSize+1;
return newDigits;
}
else
{
newDigits++;
*returnSize = digitsSize;
return newDigits;
}
}