Plus One

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.

/**
 * Return an array of size *returnSize.
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* plusOne(int* digits, int digitsSize, int* returnSize) {
    for(int i = 0; i < digitsSize; i++)
    {
        if(digits[digitsSize - i - 1] == 9)
            {
                digits[digitsSize - i - 1] = 0;
                if(i == digitsSize)
                   {
                        returnSize = malloc((digitsSize + 1) * sizeof(int));
                        memset(returnSize, 0, sizeof(returnSize));
                        returnSize[0] = 1;
                        return returnSize;
                   }
                   
            }
        else
            {
                digits[digitsSize - i – 1] += 1;
                returnSize = malloc((digitsSize) * sizeof(int));
                memcpy(returnSize, digits, digitsSize*sizeof(int));
                return returnSize;
            }
    }
}

这是我的代码,不知道为什么输出错误

Your input
[0]
Your answer
[]
Expected answer
[1]
 
自己本地测试,返回值应该是对的。。。。
posted @ 2015-09-22 09:54  dylqt  阅读(195)  评论(0编辑  收藏  举报