当前位置:网站首页>349. 两个数组的交集
349. 两个数组的交集
2022-06-09 09:28:00 【Mr Gao】
349. 两个数组的交集
给定两个数组 nums1 和 nums2 ,返回 它们的交集 。输出结果中的每个元素一定是 唯一 的。我们可以 不考虑输出结果的顺序 。
示例 1:
输入:nums1 = [1,2,2,1], nums2 = [2,2]
输出:[2]
示例 2:
输入:nums1 = [4,9,5], nums2 = [9,4,9,8,4]
输出:[9,4]
解释:[4,9] 也是可通过的
这题其实也比较简单,我们设置一个辅助数组就可以解决问题:
解题代码如下:
/** * Note: The returned array must be malloced, assume caller calls free(). */
int* intersection(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize){
int* a=(int* )malloc(sizeof(int)*1000);
int* b=(int* )malloc(sizeof(int)*1000);
int i=0;
for(i=0;i<1000;i++){
a[i]=0;
}
for(i=0;i<nums1Size;i++){
if(a[nums1[i]]==0){
a[nums1[i]]=1;
}
}
int p=0;
for(i=0;i<nums2Size;i++){
if(a[nums2[i]]==1){
a[nums2[i]]++;
b[p++]=nums2[i];
}
}
*returnSize=p;
return b;
}
边栏推荐
- [probability theory] correlation calculation between variables
- Machine learning notes - explore the keras dataset
- Openstack explanation (XV) -- basic principle of openstack Nova node
- Openstack explanation (XIV) -- Grace keystone registration
- Openstack explanation (16) -- openstack Nova installation and database configuration
- Openstack explanation (13) -- Grace keystone setup and startup
- Openstack explanation (12) -- glance installation and Preliminary Configuration
- 【genius_platform软件平台开发】第三十七讲:网卡混杂模式和原始套接字
- 数字图像处理 图解图像恢复任务
- Openstack explanation (17) -- other configurations of openstack Nova
猜你喜欢

最小路径和

【genius_platform软件平台开发】第三十七讲:网卡混杂模式和原始套接字

【新手上路常见问答】平面设计的基本原则

MSF模块查找详解

Extensions attribute of TS generics

WebService service call

MSF information collection based on FTP protocol
![[FAQ for novices on the road] how do non IT enterprises make Internet products](/img/1e/541eb4e745ab01491319e427475271.png)
[FAQ for novices on the road] how do non IT enterprises make Internet products

MSF information collection based on TCP protocol

【科技、商业和管理】看剧学创业:《硅谷》第六季第1-2集
随机推荐
[recommendation system] user based collaborative filtering
八、线性规划 顶点、极值点和基本可行解决方案
数据科学的道德与法规知识
SSM详解
[FAQ for novices on the road] how do non IT enterprises make Internet products
C # introductory series (IX) -- method use
【脑洞大开】创业的知识型企业如何招贤纳士?
【genius_platform软件平台开发】第三十七讲:网卡混杂模式和原始套接字
MSF基于SNMP协议的信息收集
[good book recommendation] popular science book of chip industry: core affairs
openstack详解(十二)——Glance安装与初步配置
MSF基于FTP协议的信息收集
【新手上路常见问答】关于物联网设计
【好书推荐】芯片产业科普书籍:《芯事》
MSF实战——ms17-010漏洞的危害
视觉SLAM总结——SuperPoint / SuperGlue
Extensions attribute of TS generics
Openstack explanation (XI) -- openstack grace service theoretical knowledge
MSF backdoor attack against FTP
Solve the apscheduler error: run time of job... Next run at:...) "was missed by