【LeetCode】求解:删除排序数组中的重复项 疑问

删除排序数组中的重复项

给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。

不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。

示例 1:

给定数组 nums = [1,1,2],

函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。

你不需要考虑数组中超出新长度后面的元素。

示例 2:

给定 nums = [0,0,1,1,1,2,2,3,3,4],

函数应该返回新的长度 5, 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4。

你不需要考虑数组中超出新长度后面的元素。

说明:

为什么返回数值是整数,但输出的答案是数组呢?

请注意,输入数组是以“引用”方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。

你可以想象内部操作如下:

// nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
int len = removeDuplicates(nums);

// 在函数里修改输入数组对于调用者是可见的。
// 根据你的函数返回的长度, 它会打印出数组中该长度范围内的所有元素。
for (int i = 0; i < len; i++) {
printf(nums[i]);
}​

#include "stdio.h"

int removeDuplicates(int* nums, int numsSize)
{
    int i, k;
    int len = numsSize;

    if (nums == NULL || numsSize == 0) 
    {
	return 0;
    }

    for (i = 0; i < len;)
    {
        if (nums[i] == nums[i+1])
	{
	    for (k = i; k < len; k++) //将nums[j]后面的元素前移一位
		nums[k] = nums[k+1];
	    len--;
	}
        else
        {
            i++;
            continue;
        }
   }

   for (int i = 0; i < len; i++) 
   {
	printf("num[%d] = %d\n", i, nums[i]);
   }

   return len;

}

int main(void)
{
    //int t[] = { 0,0,2,3,3,3,4,4,5,5 };
    //int  t[] = {0, 0, 1, 1, 1, 2, 2, 3, 3, 4};
    int t[] = {1,1,2};
    printf("the result is %d\n", removeDuplicates(t, sizeof(t)/sizeof(t[0])));

    getchar();
    return 0;
}

程序在leetcode平台运行总是报错,没找到原因,但在vs2017中就没有报错,请大家帮忙找找看看原因。

谢谢。

附带leetcode平台报错信息

=================================================================
==29==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60200000001c at pc 0x00000040199a bp 0x7ffebf02e5f0 sp 0x7ffebf02e5e8
READ of size 4 at 0x60200000001c thread T0
    #2 0x7f733bb1d2e0 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x202e0)
0x60200000001c is located 0 bytes to the right of 12-byte region [0x602000000010,0x60200000001c)
allocated by thread T0 here:
    #0 0x7f733d3cc2b0 in malloc (/usr/local/lib64/libasan.so.5+0xe82b0)
    #3 0x7f733bb1d2e0 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x202e0)
Shadow bytes around the buggy address:
  0x0c047fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c047fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c047fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c047fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c047fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c047fff8000: fa fa 00[04]fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8010: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==29==ABORTING

 

leetcode上 用calloc分配内存,有时会报错,有时能编译通过,我还啥也没干呢就报错,求大神解答!

03-18
C语言用calloc分配内存,有时会报错,有时能编译通过,我还啥也没干呢就报错,同样的代码第一次点能通过,再点就报错了,之后再点偶尔还能通过,求大神帮忙看看 int sumnum(int numsSize) //计算全排列的个数 { int ret =1; for (int i = 1; i <= numsSize; i++) { ret = ret * i; } return ret; } int** permute(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) { int sum = 0; sum = sumnum(numsSize); int **a = (int **)calloc(sum, sizeof(int *)); int *book = (int *)calloc(numsSize, sizeof(int)); for(int i = 0;i<sum;i++) { a[i] = (int*)calloc(numsSize,sizeof(int)); } return a; } 报错信息:================================================================= ==29==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x603000000028 at pc 0x00000040500d bp 0x7ffd53c29910 sp 0x7ffd53c29900 READ of size 4 at 0x603000000028 thread T0 #3 0x7fcd58c1d82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f) 0x603000000028 is located 0 bytes to the right of 24-byte region [0x603000000010,0x603000000028) allocated by thread T0 here: #0 0x7fcd59c3917e in calloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10c17e) #3 0x7fcd58c1d82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f) Shadow bytes around the buggy address: 0x0c067fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c067fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c067fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c067fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c067fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =>0x0c067fff8000: fa fa 00 00 00[fa]fa fa 00 00 00 fa fa fa 00 00 0x0c067fff8010: 00 fa fa fa 00 00 00 fa fa fa 00 00 00 fa fa fa 0x0c067fff8020: 00 00 00 fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c067fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c067fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c067fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01 02 03 04 05 06 07 Heap left redzone: fa Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 Container overflow: fc Array cookie: ac Intra object redzone: bb ASan internal: fe Left alloca redzone: ca Right alloca redzone: cb Shadow gap: cc ==29==ABORTING
©️2020 CSDN 皮肤主题: Age of Ai 设计师: meimeiellie 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值