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

// 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;
}


=================================================================
==29==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60200000001c at pc 0x00000040199a bp 0x7ffebf02e5f0 sp 0x7ffebf02e5e8
#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)
#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)
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
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
Intra object redzone:    bb
ASan internal:           fe
Left alloca redzone:     ca
Right alloca redzone:    cb
==29==ABORTING

06-23 256

05-10 209

05-24 220

01-13 191

03-26 22

07-22 100

05-29 102

09-14 80

08-05 114

#### 疑问 Leetcode 215. 数组中的第K个最大元素 堆缓存访问溢出

©️2020 CSDN 皮肤主题: Age of Ai 设计师: meimeiellie

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