码迷,mamicode.com
首页 > 其他好文 > 详细

Plus One

时间:2015-09-22 10:11:38      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

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]
 
自己本地测试,返回值应该是对的。。。。

Plus One

标签:

原文地址:http://www.cnblogs.com/dylqt/p/4828089.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!