原题:
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
You may assume the integer do not contain any leading zero, except the number 0 itself.
The digits are stored such that the most significant digit is at the head of the list.
代码如下:(C)
int* plusOne(int* digits, int digitsSize, int* returnSize) {
for(int n=digitsSize-1;n>=0;n--)
{
if(*(digits+n)==9)
{
*(digits+n)=0;
}
else
{
*(digits+n)=*(digits+n)+1;
break;
}
}
if((*digits)==0)
{
*returnSize=digitsSize+1;
int *result;
result=(int *)malloc(sizeof(int)*(*returnSize));
*result=1;
for(int n=0;n<digitsSize;n++)
{
*(result+n+1)=*(digits+n);
}
return result;
}
*returnSize=digitsSize;
return digits;
}
看下性能。
这个就是个基本功,没啥特别要说的吧感觉。