user-0 提问: leetcode plus one in c

回复:
user: user-0
content: 引用 18 楼 chengye_xin 的回复:话说楼主你这道题目PASS了么，遇到你一样的问题了，刚刚AC。  函数形参里面的那个int * returnSize 应该是你返回的int数组的长度。 A了，觉得很多题目用c写的难度远远大于题本身的难度

user: user-1
content: 话说楼主你这道题目PASS了么，遇到你一样的问题了，刚刚AC。  函数形参里面的那个int * returnSize 应该是你返回的int数组的长度。

user: user-2
content: 引用 14 楼 fly_dragon_fly 的回复:[quote=引用 13 楼 zhao4zhong1 的回复:] 其实最好的策略应该是在digits中将各位数字倒着存放。这样就可以避免有进位时还得逐位拷贝了。  No program run faster than no program!《观止-微软创建NT和未来的夺命狂奔》拷那么多注释何必了, 看看题要求吧 Note: The returned array must be malloced, assume caller calls free(),如果digits是栈地址,怎么realloc[/quote] 你见过哪段代码将栈地址在参数中用指针传递？ malloced的意思就是调用malloc函数？！ 千句解释不如一句举例。

user: user-2
content: 我说“千句解释不如一句举例。”的意思不是让你给我举“将栈地址在参数中用指针传递”的例子。 是说我注释中将函数功能解释和举例的很到位，如果不举例，只有解释是会被不同的人理解成不同具体实现的。  你竟然可以象下面这样理解： The returned array must be malloced, but the parameter digits can be global variable's address or local variable's address or alloca memory's address ?! 那你就只能后果自负了。

user: user-3
content: 引用 15 楼 zhao4zhong1 的回复:[quote=引用 14 楼 fly_dragon_fly 的回复:] [quote=引用 13 楼 zhao4zhong1 的回复:] 其实最好的策略应该是在digits中将各位数字倒着存放。这样就可以避免有进位时还得逐位拷贝了。  No program run faster than no program!《观止-微软创建NT和未来的夺命狂奔》拷那么多注释何必了, 看看题要求吧 Note: The returned array must be malloced, assume caller calls free(),如果digits是栈地址,怎么realloc[/quote] 你见过哪段代码将栈地址在参数中用指针传递？ malloced的意思就是调用malloc函数？！ 千句解释不如一句举例。[/quote]栈地址还要举例, 那就再举一下 int a[]={1,2,3}, la; plus(a,3,&la); 觉得栈地址不好,那就定义成全局.

user: user-3
content: 引用 13 楼 zhao4zhong1 的回复:其实最好的策略应该是在digits中将各位数字倒着存放。这样就可以避免有进位时还得逐位拷贝了。  No program run faster than no program!《观止-微软创建NT和未来的夺命狂奔》拷那么多注释何必了, 看看题要求吧 Note: The returned array must be malloced, assume caller calls free(),如果digits是栈地址,怎么realloc

user: user-2
content: 其实最好的策略应该是在digits中将各位数字倒着存放。这样就可以避免有进位时还得逐位拷贝了。  No program run faster than no program!《观止-微软创建NT和未来的夺命狂奔》

user: user-2
content: 引用 10 楼 fly_dragon_fly 的回复:[quote=引用 7 楼 u012042752 的回复:] [quote=引用 4 楼 fly_dragon_fly 的回复:] 模拟加1是吧, 本地能够通过就奇怪了, returnSize不对,这个是用来返回长度, 你用来放数字了.我想本地能够通过的就是这样 int a[]={1,2,3},la=3; plus(a,la,&la), 改成la=1234;plus(a,3,&la) 或者试试int a[]={9,9,9},就是需要进位的时候然而本地0，9，｛9，9，9｝都通过了[/quote]可能测试代码有问题, 999是不可能通过测试的,不过既然已经结贴就不评论了,赵四的break是错的,那本来就不应该叫count,是carry,下面重写一下 // if (count==1) *returnSize=digitsSize+count; int *rd =malloc(*returnSize*sizeof(*rd)); if(count) rd[0] =1; memcpy(rd+count,digits,digitsSize*sizeof(*rd)); return rd;[/quote] 返回新malloc的rd了，原来digits不free？内存泄露了！ 新malloc，你还得memcpy，不嫌费劲吗？ 这里应该用realloc realloc Reallocate memory blocks.  void *realloc( void *memblock, size_t size );  Routine Required Header Compatibility  realloc <stdlib.h> and <malloc.h> ANSI, Win 95, Win NT    For additional compatibility information, see Compatibility in the Introduction.  Libraries  LIBC.LIB Single thread static library, retail version  LIBCMT.LIB Multithread static library, retail version  MSVCRT.LIB Import library for MSVCRT.DLL, retail version    Return Value  realloc returns a void pointer to the reallocated (and possibly moved) memory block. The return value is NULL if the size is zero and the buffer argument is not NULL, or if there is not enough available memory to expand the block to the given size. In the first case, the original block is freed. In the second, the original block is unchanged. The return value points to a storage space that is guaranteed to be suitably aligned for storage of any type of object. To get a pointer to a type other than void, use a type cast on the return value.  Parameters  memblock  Pointer to previously allocated memory block  size  New size in bytes  Remarks  The realloc function changes the size of an allocated memory block. The memblock argument points to the beginning of the memory block. If memblock is NULL, realloc behaves the same way as malloc and allocates a new block of size bytes. If memblock is not NULL, it should be a pointer returned by a previous call to calloc, malloc, or realloc.  The size argument gives the new size of the block, in bytes. The contents of the block are unchanged up to the shorter of the new and old sizes, although the new block can be in a different location. Because the new block can be in a new memory location, the pointer returned by realloc is not guaranteed to be the pointer passed through the memblock argument.  realloc calls malloc in order to use the C++ _set_new_mode function to set the new handler mode. The new handler mode indicates whether, on failure, malloc is to call the new handler routine as set by _set_new_handler. By default, malloc does not call the new handler routine on failure to allocate memory. You can override this default behavior so that, when realloc fails to allocate memory, malloc calls the new handler routine in the same way that the new operator does when it fails for the same reason. To override the default, call   _set_new_mode(1)  early in your program, or link with NEWMODE.OBJ.  When the application is linked with a debug version of the C run-time libraries, realloc resolves to _realloc_dbg. For more information about how the heap is managed during the debugging process, see Using C Run-Time Library Debugging Support.  Example  /* REALLOC.C: This program allocates a block of memory for  * buffer and then uses _msize to display the size of that  * block. Next, it uses realloc to expand the amount of  * memory used by buffer and then calls _msize again to  * display the new amount of memory allocated to buffer.  */  #include <stdio.h> #include <malloc.h> #include <stdlib.h>  void main( void ) {    long *buffer;    size_t size;     if( (buffer = (long *)malloc( 1000 * sizeof( long ) )) == NULL )       exit( 1 );     size = _msize( buffer );    printf( "Size of block after malloc of 1000 longs: %u\n", size );     /* Reallocate and show new size: */    if( (buffer = realloc( buffer, size + (1000 * sizeof( long )) ))          ==  NULL )       exit( 1 );    size = _msize( buffer );    printf( "Size of block after realloc of 1000 more longs: %u\n",              size );     free( buffer );    exit( 0 ); }   Output  Size of block after malloc of 1000 longs: 4000 Size of block after realloc of 1000 more longs: 8000   Memory Allocation Routines  See Also   calloc, free, malloc

user: user-0
content: 引用 10 楼 fly_dragon_fly 的回复:[quote=引用 7 楼 u012042752 的回复:] [quote=引用 4 楼 fly_dragon_fly 的回复:] 模拟加1是吧, 本地能够通过就奇怪了, returnSize不对,这个是用来返回长度, 你用来放数字了.我想本地能够通过的就是这样 int a[]={1,2,3},la=3; plus(a,la,&la), 改成la=1234;plus(a,3,&la) 或者试试int a[]={9,9,9},就是需要进位的时候然而本地0，9，｛9，9，9｝都通过了[/quote]可能测试代码有问题, 999是不可能通过测试的,不过既然已经结贴就不评论了,赵四的break是错的,那本来就不应该叫count,是carry,下面重写一下 // if (count==1) *returnSize=digitsSize+count; int *rd =malloc(*returnSize*sizeof(*rd)); if(count) rd[0] =1; memcpy(rd+count,digits,digitsSize*sizeof(*rd)); return rd;[/quote] 谢谢，昨天自己改了改通过了

user: user-3
content: 引用 7 楼 u012042752 的回复:[quote=引用 4 楼 fly_dragon_fly 的回复:] 模拟加1是吧, 本地能够通过就奇怪了, returnSize不对,这个是用来返回长度, 你用来放数字了.我想本地能够通过的就是这样 int a[]={1,2,3},la=3; plus(a,la,&la), 改成la=1234;plus(a,3,&la) 或者试试int a[]={9,9,9},就是需要进位的时候然而本地0，9，｛9，9，9｝都通过了[/quote]可能测试代码有问题, 999是不可能通过测试的,不过既然已经结贴就不评论了,赵四的break是错的,那本来就不应该叫count,是carry,下面重写一下 // if (count==1) *returnSize=digitsSize+count; int *rd =malloc(*returnSize*sizeof(*rd)); if(count) rd[0] =1; memcpy(rd+count,digits,digitsSize*sizeof(*rd)); return rd;

user: user-0
content: 引用 5 楼 zhao4zhong1 的回复:楼主找找差距吧： /**  * array digits[0..digitsSize-1] store a long decimal digits  * function plusOne plus 1 to it.  * whether carried or not, resize or not resize digits, return new size to *returnSize  # For example:  *     if digits[2]={9,8} plusOne return digits[2]={9,9} and *returnSize=2  *     if digits[2]={9,9} plusOne return digits[3]={1,0,0} and *returnSize=3  * Note: The returned array must be malloced, assume caller calls free().  */ int* plusOne(int* digits, int digitsSize, int* returnSize) {     int i;     int count=1;     int *digits1;      assert(digits);     assert(digitsSize>0);     assert(returnSize);     for (i=digitsSize-1;i>=0;i--) {         int a = digits[i] + count;         digits[i] = a % 10;         count = a / 10;         if (0==count) break;     }     if (count==1) {         digitsSize++;         digits1 =(int*)realloc(digits,sizeof(int)*digitsSize);         assert(digits1);         for (i=digitsSize-2; i>=0; i--) digits1[i+1]=digits1[i];         digits1[0]=1;         *returnSize=digitSize;         return digits1;     } else {         *returnSize=digitSize;         return digits;     } } 谢谢，正在努力缩小差距：）

user: user-0
content: 引用 6 楼 mewiteor 的回复:Return an array of size *returnSize. *returnSize是用来记录返回数组的大小的,大概测试程序将*returnSize初始化为0了,这样Output会永远为空. 应该是: count==1时,*returnSize=digitsSize+1; count==0时,*returnSize=digitsSize; 不要改变returnSize,只能改变*returnSize. 另外定义一个int型指针用于返回.好像明白一点了，3ks～

user: user-0
content: 引用 4 楼 fly_dragon_fly 的回复:模拟加1是吧, 本地能够通过就奇怪了, returnSize不对,这个是用来返回长度, 你用来放数字了.我想本地能够通过的就是这样 int a[]={1,2,3},la=3; plus(a,la,&la), 改成la=1234;plus(a,3,&la) 或者试试int a[]={9,9,9},就是需要进位的时候然而本地0，9，｛9，9，9｝都通过了

user: user-4
content: Return an array of size *returnSize. *returnSize是用来记录返回数组的大小的,大概测试程序将*returnSize初始化为0了,这样Output会永远为空. 应该是: count==1时,*returnSize=digitsSize+1; count==0时,*returnSize=digitsSize; 不要改变returnSize,只能改变*returnSize. 另外定义一个int型指针用于返回.

user: user-2
content: 楼主找找差距吧： /**  * array digits[0..digitsSize-1] store a long decimal digits  * function plusOne plus 1 to it.  * whether carried or not, resize or not resize digits, return new size to *returnSize  # For example:  *     if digits[2]={9,8} plusOne return digits[2]={9,9} and *returnSize=2  *     if digits[2]={9,9} plusOne return digits[3]={1,0,0} and *returnSize=3  * Note: The returned array must be malloced, assume caller calls free().  */ int* plusOne(int* digits, int digitsSize, int* returnSize) {     int i;     int count=1;     int *digits1;      assert(digits);     assert(digitsSize>0);     assert(returnSize);     for (i=digitsSize-1;i>=0;i--) {         int a = digits[i] + count;         digits[i] = a % 10;         count = a / 10;         if (0==count) break;     }     if (count==1) {         digitsSize++;         digits1 =(int*)realloc(digits,sizeof(int)*digitsSize);         assert(digits1);         for (i=digitsSize-2; i>=0; i--) digits1[i+1]=digits1[i];         digits1[0]=1;         *returnSize=digitSize;         return digits1;     } else {         *returnSize=digitSize;         return digits;     } }

user: user-3
content: 模拟加1是吧, 本地能够通过就奇怪了, returnSize不对,这个是用来返回长度, 你用来放数字了.我想本地能够通过的就是这样 int a[]={1,2,3},la=3; plus(a,la,&la), 改成la=1234;plus(a,3,&la) 或者试试int a[]={9,9,9},就是需要进位的时候

user: user-5
content: 不明白要干吗

user: user-6
content: input [0] 在执行到if(count == 1)时，digits[0] = 1没错，可是此时count = 0，程序直接跳到else中 此时returnSize未初始化，我估计leetcode是通过returnSize的值来取返回值

user: user-2
content: 不明白要完成什么功能。