问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4042 访问。

给定两个数组,编写一个函数来计算它们的交集。

输入: nums1 = [1,2,2,1], nums2 = [2,2]

输出: [2]

输入: nums1 = [4,9,5], nums2 = [9,4,9,8,4]

输出: [9,4]

说明:

输出结果中的每个元素一定是唯一的。
我们可以不考虑输出结果的顺序。


Given two arrays, write a function to compute their intersection.

Input: nums1 = [1,2,2,1], nums2 = [2,2]

Output: [2]

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]

Output: [9,4]

Note:

Each element in the result must be unique.
The result can be in any order.


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4042 访问。

public class Program {public static void Main(string[] args) {var nums1 = new int[] { 1, 2, 2, 1 };var nums2 = new int[] { 2, 2 };var res = Intersection(nums1, nums2);ShowArray(res);nums1 = new int[] { 4, 9, 5 };nums2 = new int[] { 9, 4, 9, 8, 4 };res = Intersection2(nums1, nums2);ShowArray(res);Console.ReadKey();}private static void ShowArray(int[] array) {foreach(var num in array) {Console.Write($"{num} ");}Console.WriteLine();}private static int[] Intersection(int[] nums1, int[] nums2) {var set = new HashSet<int>();foreach(var i in nums1) {if(!set.Contains(i)) set.Add(i);}var list = new List<int>();var set2 = new HashSet<int>();foreach(var k in nums2) {if(set.Contains(k) && !set2.Contains(k)) {list.Add(k);set2.Add(k);}}return list.ToArray();}private static int[] Intersection2(int[] nums1, int[] nums2) {return nums1.Intersect(nums2).ToArray();}}

以上给出2种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4042 访问。

2
4 9

分析:

显而易见,以上2种算法的时间复杂度均为:  。

C#LeetCode刷题之#349-两个数组的交集(Intersection of Two Arrays)相关推荐

  1. LeetCode算法题350:两个数组的交集II(Intersection of Two Arrays II)

    技术交流可以加: 本人微信:xcg852390212 本人qq:852390212 学习交流qq群1(已满): 962535112 学习交流qq群2: 780902027 两个数组的交集II Leet ...

  2. 两个对象数组交集_yiduobo的每日leetcode 349.两个数组的交集 amp;amp; 350.两个数组的交集II...

    祖传的手艺不想丢了,所以按顺序写一个leetcode的题解.计划每日两题,争取不卡题吧 349.两个数组的交集https://leetcode-cn.com/problems/intersection ...

  3. Leetcode刷题100天—349. 两个数组的交集(集合)—day08

    前言: 作者:神的孩子在歌唱 大家好,我叫运智 349. 两个数组的交集 难度简单410收藏分享切换为英文接收动态反馈 给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入:nums1 = ...

  4. 算法训练Day6 | LeetCode:242. 有效的字母异位词(数组作哈希表);349. 两个数组的交集(Set作哈希表);202.快乐数 (Set作哈希表);1. 两数之和(Map作哈希表)

    目录 LeetCode242. 有效的字母异位词 方法:数组作哈希表 1. 思路 2. 代码实现 3. 复杂度分析 4. 思考 Leetcode349. 两个数组的交集 方法一:用Set作HashMa ...

  5. 小白刷代码随想录day6 -- 242.有效的字母异位词,349.两个数组的交集,202快乐数,1.两数之和

    今天是刷代码随想录的day6.昨天day5元宵节休息日.今天开始了哈希表部分.对于哈希表的内容之前没有怎么学习过,所以今天的刷题主要以学习方法为主.待二刷的时候争取能够手撕代码! 哈希法 首先一个大前 ...

  6. LeetCode -349 两个数组的交集

    难度:简单 给定两个数组 nums1 和 nums2 ,返回它们的交集 .输出结果中的每个元素一定是唯一 的.我们可以不考虑输出结果的顺序 . 题目链接 LeetCode -349 两个数组的交集 S ...

  7. LeetCode 349 两个数组的交集

    349. 两个数组的交集 难度简单476收藏分享切换为英文接收动态反馈 给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入:nums1 = [1,2,2,1], nums2 = [2,2] ...

  8. LeetCode 349. 两个数组的交集【哈希表】

    349. 两个数组的交集 给定两个数组 nums1 和 nums2 ,返回 它们的交集 .输出结果中的每个元素一定是 唯一 的.我们可以 不考虑输出结果的顺序 . 示例 1: 输入:nums1 = [ ...

  9. 349. 两个数组的交集 golang

    349. 两个数组的交集 给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2] 示例 2: 输入: num ...

  10. Day 06 | 242.有效的字母异位词 349. 两个数组的交集 202.快乐数 1. 两数之和

    这四道题之前都做过!比第一次顺利一些,不过还是得看题解,磕磕绊绊的.不过我相信坚持下去一定会越来越好滴! 242.有效的字母异位词 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的 ...

最新文章

  1. LPMS_IMU在TX2上使用
  2. JavaWeb 入门篇(1)Maven创建Web项目 Idea配置tomcat
  3. ES6箭头函数中的this指向
  4. JavaFX UI控件教程(二十三)之Menu
  5. 微信小程序 调用地图接口,实现定位
  6. java的text函数,excel text函数以及相关的函数使用方法
  7. ubuntu物理机上搭建Kubernetes集群 -- 准备
  8. systemd和sysv服务管理和配置
  9. iOS 一种很方便的构造TarBar
  10. 苹果核 - iOS端Mock GPS定位 —— 测试、开发、玩游戏、发朋友圈等等,你都用得上...
  11. latex表格手把手超详细教程(table, tabular, multirow, multicolumn)
  12. sd卡的速度怎么测试软件,【有图】怎么测SD卡的速度-蜂鸟网
  13. html阅读模式怎么进入word模式,word阅读模式怎么取消
  14. 虚拟机ping百度失败:PING www.a.shifen.com (163.177.151.110) 56(84) bytes of data.光标就一直闪
  15. 仅以此文敬我们可歌可泣的2007年
  16. 同一工作组计算机无法查看,win7系统在同一个工作组看不到其他电脑怎么回事?...
  17. 人力资源实习生是什么岗位
  18. Nginx 代理minio 共享文件
  19. 使用k-means聚类anchors
  20. 支持向量机(一)——线性可分支持向量机

热门文章

  1. LeetCode 121. Best Time to Buy and Sell Stock
  2. 异常的捕获 try...catch java
  3. 接口目标 java 1614953528
  4. linux-基本权限UGO-读写执行权限
  5. sublime-编辑器的使用-基本
  6. python-去重的三种方式-成员判断-索引判断-集合
  7. oracel 创建视图给某个用户
  8. LeetCode —— 深搜水题记录
  9. 必备知识:大数据处理应遵循的原则
  10. 用JS写的无缝滚动特效