/*
* @lc app=leetcode.cn id=349 lang=c
*
* [349] 两个数组的交集
*
* https://leetcode-cn.com/problems/intersection-of-two-arrays/description/
*
* algorithms
* Easy (60.49%)
* Total Accepted: 15.1K
* Total Submissions: 25K
* Testcase Example: '[1,2,2,1]\n[2,2]'
*
* 给定两个数组,编写一个函数来计算它们的交集。
*
* 示例 1:
*
* 输入: nums1 = [1,2,2,1], nums2 = [2,2]
* 输出: [2]
*
*
* 示例 2:
*
* 输入: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
* 输出: [9,4]
*
* 说明:
*
*
* 输出结果中的每个元素一定是唯一的。
* 我们可以不考虑输出结果的顺序。
*
*
*/
/**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
int* intersection(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize) {
int i,j,k=,flag=;
int lens=nums1Size>nums2Size?nums1Size:nums2Size;
int* re =(int*)malloc(sizeof(int)*lens);
if(nums1Size==) { * returnSize=k; return nums1; }
if(nums2Size==) { * returnSize=k; return nums2; }
for(i=;i<nums1Size;i++)
{//flag用来标识数组re中是否已经存在nums1和nums2的交集,flag==0,则只要比较nums2[j]==nums1[i]
if(flag==)
for(j=;j<nums2Size;j++)
{ if(nums2[j]==nums1[i])
{flag=;re[k++]=nums1[i];break;}
}
//flag==1说明re中已存在交集数字,则nums1[i]还需要与re数组中的每个元素进行比较,保证输出结果中的每个元素一定是唯一的
else if(flag==)
{int t,f=;
for (t=;t<k;t++)
if(nums1[i]==re[t]) { f=;break;}
if(f==) continue;
for(j=;j<nums2Size;j++)
{
if(nums2[j]==nums1[i])
{ flag=;re[k++]=nums1[i];break;}
}
}
}
* returnSize=k;
return re;
}

思路是 建立第三个数组,其长度为两个数组中较小的长度的那个。然后判断是否空集。(这么理解吧=。=)

然后就是 在第二个数组中逐一选择与第一个数组中数对比,如果相等的话就存入第三个数组。flag用来标识数组re中是否已经存在nums1和nums2的交集,flag==0,则只要比较nums2[j]==nums1[i]

flag==1说明re中已存在交集数字,则nums1[i]还需要与re数组中的每个元素进行比较,保证输出结果中的每个元素一定是唯一的

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

python:

#
# @lc app=leetcode.cn id=349 lang=python3
#
# [349] 两个数组的交集
#
# https://leetcode-cn.com/problems/intersection-of-two-arrays/description/
#
# algorithms
# Easy (60.49%)
# Total Accepted: 15.1K
# Total Submissions: 25K
# Testcase Example: '[1,2,2,1]\n[2,2]'
#
# 给定两个数组,编写一个函数来计算它们的交集。
#
# 示例 1:
#
# 输入: nums1 = [1,2,2,1], nums2 = [2,2]
# 输出: [2]
#
#
# 示例 2:
#
# 输入: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
# 输出: [9,4]
#
# 说明:
#
#
# 输出结果中的每个元素一定是唯一的。
# 我们可以不考虑输出结果的顺序。
#
#
#
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
return list(set(nums1) & set(nums2))

python就很骚,直接两个set去重然后一个&取交集。

Leecode刷题之旅-C语言/python-349两个数组的交集的更多相关文章

  1. Leecode刷题之旅-C语言/python-88合并两个有序数组

    /* * @lc app=leetcode.cn id=88 lang=c * * [88] 合并两个有序数组 * * https://leetcode-cn.com/problems/merge-s ...

  2. Leecode刷题之旅-C语言/python-21.合并两个有序链表

    /* * @lc app=leetcode.cn id=21 lang=c * * [21] 合并两个有序链表 * * https://leetcode-cn.com/problems/merge-t ...

  3. Leecode刷题之旅-C语言/python-67二进制求和

    /* * @lc app=leetcode.cn id=67 lang=c * * [67] 二进制求和 * * https://leetcode-cn.com/problems/add-binary ...

  4. Leecode刷题之旅-C语言/python-1.两数之和

    开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...

  5. Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符

    /* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...

  6. Leecode刷题之旅-C语言/python-28.实现strstr()

    /* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...

  7. Leecode刷题之旅-C语言/python-7.整数反转

    /* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...

  8. Leecode刷题之旅-C语言/python-434 字符串中的单词数

    /* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...

  9. Leecode刷题之旅-C语言/python-326 3的幂

    /* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...

随机推荐

  1. windows下安装配置RabbitMQ

    安装部署 1.当前环境以及参考资料出处 部署环境:windows server 2008 r2 enterprise 官方安装部署文档:http://www.rabbitmq.com/install- ...

  2. 配置docker容器上ssh无密登录

    配置docker容器上ssh无密登录 1.修改所有容器中root账户密码 ssh到远程主机时,首次需要密码访问,因此需要修改root账号密码. 密码必须要8位以上字母数字混合. $>passwd ...

  3. windows中实现有相同的程序运行就不在运行新的程序。

    主要是通过互斥量内核对象来实现程序间互斥. // CEcopClientApp 初始化HANDLE m_hMutexMark = NULL;BOOL CEcopClientApp::InitInsta ...

  4. LambdaToSql(轻量级ORM) 入门篇 开源项目

    为什么开发(背景) 最开始使用的是 sqlDbHelper,有微软的,有自己写的. 后来开始使用比较成熟的框架开发,使用过一段时间的Hibernate,后期主要使用 Entity FrameWork. ...

  5. 如何使用cntlm配置代理上网

    https://blog.csdn.net/SdustLiYang/article/details/7034974 https://blog.csdn.net/bluishglc/article/de ...

  6. ZT 七大寡头

    网易评论人才辈出啊!!!看下面   http://comment.news.163.com/news_guoji2_bbs/9GRIIJA90001121M.html 关注 关注他的微博yftyfm ...

  7. ThreadLocal介绍

    作者:知乎用户链接:https://www.zhihu.com/question/23089780/answer/62097840来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  8. reactnative调研

    /**   * This function parses the exported methods inside RCTBridgeModules and   * generates an array ...

  9. UVa 1220 - Party at Hali-Bula(树形DP)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  10. virtualbox 安装 mac os x lion 10.7实现全屏显示!

    1. 启动Virtual Box虚拟机,在虚拟机里编辑 /Library/Preferences/SystemConfiguration/com.apple.Boot.plist,找到 <dic ...