Leetcode错误记录

本文详细记录了LeetCode中遇到的编程错误,包括数组越界、未正确遍历、函数参数兼容性问题和整数溢出。针对每个错误,提供了错误原因分析和修复方案,涉及数组操作、循环条件修改、函数参数类型匹配及比较操作优化。
摘要由CSDN通过智能技术生成

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];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ycr的帐号

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值