概述
Leetcode错误笔记
1.(455:分发饼干)
错误记录:
数组越界
=================================================================
42ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6020000000f0 at pc 0x56162a87935f bp 0x7ffcac9a5b60 sp 0x7ffcac9a5b50
READ of size 4 at 0x6020000000f0 thread T0
#2 0x7f0768a6a0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
0x6020000000f1 is located 0 bytes to the right of 1-byte region [0x6020000000f0,0x6020000000f1)
allocated by thread T0 here:
#0 0x7f07696afbc8 in malloc (/lib/x86_64-linux-gnu/libasan.so.5+0x10dbc8)
#3 0x7f0768a6a0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
Shadow bytes around the buggy address:
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 00 fa fa fa 04 fa fa fa 00 fa
=>0x0c047fff8010: fa fa 00 04 fa fa 04 fa fa fa 00 04 fa fa[01]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
0x0c047fff8060: 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
==42=ABORTING
错误代码:
int cmp(const void* a, const void* b) {
return (*(int*)a - *(int*)b);
}
int findContentChildren(int* g, int gSize, int* s, int sSize) {
qsort(g, gSize, sizeof(int), cmp);
qsort(s, sSize, sizeof(int), cmp);
int child = 0;
for (int kid = 0, cookie = 0; kid < gSize && cookie < sSize; kid++, cookie++) {
while (cookie < sSize && g[kid] > s[cookie])
cookie++;
if (g[kid] <= s[cookie])//cookie可能大于sSize,导致越界
child++;
}
return child;
}
错误解决:
判断条件处可能访问越界,改变判断条件
for (int kid = 0, cookie = 0; kid < gSize && cookie < sSize; kid++, cookie++) {
while (cookie < sSize && g[kid] > s[cookie]
)
cookie++;
if (cookie < sSize)//修改判断条件
child++;
}
2.(135:分发糖果)
错误记录:
未正确遍历
错误代码:
int candy(int* ratings, int ratingsSize) {
int candies = 0;
int* a = (int*)malloc(sizeof(int) * ratingsSize);
for (int i = 0; i < ratingsSize; i++)
a[i] = 1;
for (int i = 0; i < ratingsSize-1; i++) {
if (ratings[i] > ratings[i + 1])
a[i] = a[i + 1] + 1;
}
for (int i = ratingsSize-1; i > 0; i--) {
if (ratings[i] > ratings[i - 1])
a[i] = a[i - 1] + 1;
}
for (int i = 0; i < ratingsSize; i++) {
candies += a[i];
printf("%d,",a[i]);
}
return candies;
}
错误解决:
按照贪心原则,从左向右遍历时,应优先更新右边的糖果数,这样下一次遍历时可以按照新 的改变更新,而且在从右向左再遍历的时候,应该注意是否左边糖果数已经大于右边了,否则会导致上一次白更新
int candy(int* ratings, int ratingsSize) {
int candies = 0;
int* a = (int*)malloc(sizeof(int) * ratingsSize);
for (int i = 0; i < ratingsSize; i++)
a[i] = 1;
for (int i = 0; i < ratingsSize-1; i++) {
if (ratings[i] < ratings[i + 1])
a[i + 1] = a[i] + 1;
}
for (int i = ratingsSize-1; i > 0; i--) {
if (ratings[i] < ratings[i - 1] && a[i-1] <= a[i])//优先向更新方向更新,且判断是否已经不用更新了
a[i - 1] = a[i] + 1;
}
for (int i = 0; i < ratingsSize; i++) {
candies += a[i];
printf("%d,",a[i]);
}
return candies;
}
3.(435:无重叠区间)
错误记录:
E0167 int(*)(int** a int** b)类型的实参与 _CoreCrtNonSecureSearchSortCompareFunction类型的形参不兼容
错误代码:
int cmp(int** a, int** b) {
return (*a)[1] - (*b)[1];
}
int eraseOverlapIntervals(int** intervals, int intervalsSize) {
qsort(intervals, intervalsSize, sizeof(int*), cmp);
...
}
错误解决:
qsort函数的比较函数参数在vs2019里只能是一级指针,修改后手动转换即可
int cmp(const void* a, const void* b) {
return (*((int**)a))[1] - (*((int**)b))[1];
}
4.(452: 用最少数量的箭引爆气球)
错误记录:
Line 4: Char 17: runtime error: signed integer overflow: -2147483645 - 2147483647 cannot be represented in type ‘int’ [solution.c]
错误代码:
int cmp(const void* a, const void* b) {
int* tempa = *(int**)a;
int* tempb = *(int**)b;
return tempa[1]-tempb[1];
}
int findMinArrowShots(int** points, int pointsSize, int* pointsColSize) {
int count = 0;
if (pointsSize == 1)
return 1;
qsort(points, pointsSize, sizeof(int*), cmp);
...
}
错误解决:
输入数据超过int数据类型范围,在cmp比较函数中用比较代替相减运算即可
int cmp(const void* a, const void* b) {
int* tempa = *(int**)a;
int* tempb = *(int**)b;
return tempa[1]>tempb[1];
}
最后
以上就是机灵鸡为你收集整理的Leetcode错误记录的全部内容,希望文章能够帮你解决Leetcode错误记录所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复