记录本算法小白刷力扣的这道题遇到的报错

349. 两个数组的交集https://leetcode.cn/problems/intersection-of-two-arrays/

出现报错的代码

/*** Note: The returned array must be malloced, assume caller calls free().*/
int* intersection(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize){int hash[1000]={0};int result[1000];//交集是去重的,最多只有1000个数for(int i=0;i<nums1Size;i++){//将nums1录入哈希表hash[nums1[i]]++;}int j=0;for(int i=0;i<nums2Size;i++){//将nums2与哈希表比对if(hash[nums2[i]]!=0){result[j]=nums2[i];j++;hash[nums2[i]]=0;//手动去重}}*returnSize=j;//这个是主函数输出时用的,跟内部计算关系不大return result;
}

报错描述

Line 207: Char 3: runtime error: load of null pointer of type 'int' [__Serializer__.c]

原因分析

该题需要return一个数组,而在不申请空间(不malloc)的情况下函数内建立的数组是局部变量,无法带回主函数。

解决方法

在函数里malloc需要返回的数组(其实初始代码顶部的note里说的就是这个)。注意建立数组时在中括号里写数组大小跟malloc的效果在这方面是完全不一样的。

这里用于返回的数组是result,所以在建立result数组时要malloc

修改后的代码

/*** Note: The returned array must be malloced, assume caller calls free().*/
int* intersection(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize){int hash[1000]={0};int *result=(int*)malloc(sizeof(int)*1000);//交集是去重的,最多只有1000个数for(int i=0;i<nums1Size;i++){//将nums1录入哈希表hash[nums1[i]]++;}int j=0;for(int i=0;i<nums2Size;i++){//将nums2与哈希表比对if(hash[nums2[i]]!=0){result[j]=nums2[i];j++;hash[nums2[i]]=0;//手动去重}}*returnSize=j;//这个是主函数输出时用的,跟内部计算关系不大return result;
}

将原来的     int result[1000];

改成了         int *result=(int*)malloc(sizeof(int)*1000);

只修改了这一行,虽然非常浅显但一定要知道的错误

这里因为题目数据限制为

  • 1 <= nums1.length, nums2.length <= 1000
  • 0 <= nums1[i], nums2[i] <= 1000

数字大小不会超过1000,可以直接开一个大小为1000的数组(虽然烧了不必要的内存)。实际上返回数组是去重的,题目给的两个数组的重复数据个数不会超过数据个数比较小的那个,这样会比较省内存(但我懒得写了)。

力扣报错runtime error: load of null pointer of type ‘int‘解决思路相关推荐

  1. leetcode报错:member access within null pointer of type struct ListNode

    leetcode报错:member access within null pointer of type 'struct ListNode'

  2. SAP LSMW 事务代码HUPAST的录屏后台执行报错 - Runtime error RAISE_EXCEPTION has occurred - 之分析

    SAP LSMW 事务代码HUPAST的录屏后台执行报错 - Runtime error RAISE_EXCEPTION has occurred - 之分析 因项目上成品库存管理启用了handlin ...

  3. 【项目问题】PM2管理器运行报错: Error: bind EADDRINUSE null:8360(阿里云服务器)

    本人后端是使用thinkjs项目创建的,npm run compile 编译后上传到服务器,发现报错:Error: bind EADDRINUSE null:8360 第一步:查看运行日志,报错原因是 ...

  4. Line 923: Char 9: runtime error: reference binding to null pointer of type ‘int‘ (stl_vector.h)

    Leetcode 报错 Line 923: Char 9: runtime error: reference binding to null pointer of type 'int' (stl_ve ...

  5. leetcode报错runtime error: reference binding to null pointer of type ‘std::vector<std::__cxx11::basic_

    leetcode报错:runtime error: reference binding to null pointer of type 'std::vector<std::__cxx11::ba ...

  6. 力扣报错“AddressSanitizer: heap-buffer-overflow on address...”的解决办法

    做力扣报了个错: AddressSanitizer: heap-buffer-overflow on address 0x6020000001cc at pc-- 大概意思 LeetCode使用了Ad ...

  7. LeetCode报错: “runtime error: member access within null pointer of type ‘struct ListNode”

    LeetCode里面会经常使用到ListNode这个数据结构,这个报错原因是对空指针进行了引用,但是在测试的时候并不会出现报错,只有在提交的时候才会出现报错. 两者出现区别的原因在于:在测试中给出的测 ...

  8. LeetCode报错:runtime error: member access within null pointer of type ‘struct ListNode‘

    错误题目:876. 链表的中间结点 错误原因:试图使用空指针 解决方法:找出等价判断条件进行替换,排除对空指针的引用. /*** Definition for singly-linked list.* ...

  9. git报错:error: failed to push some refs to ... 的解决办法及如何让线上覆盖本地方法【Git/SVN】

    报错代码 在 push 的时候,出现如下错误: error: failed to push some refs to 'ssh://git@git.umbrella.com:1024/test-pow ...

最新文章

  1. php 加载一个文件路径_PHP文件加载过程
  2. 参数估计_数据分析|统计之一个总体参数估计(附代码)
  3. python with函数的用法(with expression [as target])
  4. Redis _面试经典
  5. MySQL学习记录—Date函数系列
  6. php如何看phpinfo(),如何从phpinfo中获取有效信息 | 学步园
  7. python 读取文本文件_如何在Python中读取大文本文件
  8. Cisco 路由器破解密码
  9. html5给页面添加雨滴特效,JavaScript canvas实现雨滴特效
  10. 2017年Go语言入门教程-徐培成-专题视频课程
  11. 安卓开发基础知识3(国内深度摄像头方案)
  12. jmeter批量上传图片, csv文件参数化——详细讲解
  13. ubuntu慢?如何给 ubuntu 换源 提速
  14. 算法优化专题A POJ-2352
  15. kindle版java核心_深入理解java 7 核心技术与最佳实践-成富[6寸pdf mobi epub kindle版].pdf...
  16. 计算机专业屏幕尺寸,电脑屏幕尺寸怎么看
  17. Docker -- 2 -- 利用docker部署网站和数据库
  18. APP报毒的自查方法
  19. 【Python】霍兰德人格分析雷达图
  20. ABBYY FineReader 14都新增了什么功能?

热门文章

  1. 如何关闭电脑软件的开机自启动
  2. 说话快,别人感觉太强势怎么办?
  3. 字节员工吐槽发誓:以后找工作,女领导的绝对不去,网友:什么岗位?
  4. ubuntu中查看qq聊天记录
  5. Bootstrap Timepicker使用
  6. Linux/centos更改网卡名字
  7. config.user.inc.php,phpMyAdmin Docker 简单配置
  8. 课工场-JAVA高级特性编程及实战第1章练习题3答案参考
  9. nodejs socket.io 聊天室
  10. 腾讯会议关闭自动更新