C#LeetCode刷题之#349-两个数组的交集(Intersection of Two Arrays)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章
- [Swift]LeetCode349. 两个数组的交集 | Intersection of Two Arrays
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...
- Java实现 LeetCode 349 两个数组的交集
349. 两个数组的交集 给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2] 示例 2: 输入: num ...
- Leetcode 349. 两个数组的交集 By Python
给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2] 示例 2: 输入: nums1 = [4,9,5], ...
- leetcode NO.349 两个数组的交集 (python实现)
来源 https://leetcode-cn.com/problems/intersection-of-two-arrays/ 题目描述 给定两个数组,写一个函数来计算它们的交集. 例子: 给定 nu ...
- leetcode刷题笔记-1. 两数之和(java实现)
题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,数组中同一个元素不能使 ...
- leetcode刷题四<寻找两个有序数组的中位数>
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nums2 ...
- leetcode刷题第二天<两数相加>
题目描述 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表 ...
- LeetCode 刷题笔记 1. 两数之和(Two Sum)
tag: 栈(stack) 题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案. ...
- (1)leetcode刷题Python笔记——两数之和
题目如下: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数 ...
- LeetCode刷题--21.合并两个有序链表(简单)
题目描述 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1 -> 2 -> 4 ,1 -> 3 -> 4 输出:1 ...
随机推荐
- Python Ethical Hacking - Intercepting and Modifying Packets
INTERCEPTING & MODIFYING PACKETS Scapy can be used to: Create packets. Analyze packets. Send/rec ...
- 工作用不到,面试经常问,这么头疼的Spring,怎么能快速过关
目录 这次文章很简单,但是也不简单,spring,spring的IOC和AOP,不知道你掌握的怎么样,最近身边的朋友有木有去面试的?他们被问到的spring面试题你能回答吗? 一看这张图,可能有朋友会 ...
- echarts 踩坑 : id必须不同
我们可能用react前端框架开发项目. 也就是组件化开发. 一个页面里可能有很多组件. 而echarts是寻找特定ID的DOM去渲染的. 也就是说,如果整个页面.包括所有页面组件,有id相同的DOM, ...
- PyQt样式设计
QSS QSS(Qt Style Sheets)即PyQt样式表,是用来定义控件外观的一种机制.QSS内部实现大量参考了CSS,但是功能没有CSS强大,主要体现在选择器少,属性少等. 使用QSS 格式 ...
- static关键字和final关键字
static关键字和final关键字 static(静态) 作用 用来修饰属性.方法.代码块.内部类 static修饰属性 表示静态变量(类变量) 按是否使用static修饰,属性的分类 静态属性 当 ...
- Presto 函数开发
0. 写在前面 Presto Functions 并不能像 Hive UDF 一样动态加载,需要根据 Function 的类型,实现 Presto 内部定义的不同接口,在 Presto 服务启动时进行 ...
- 服务质量分析:腾讯会议&腾讯云Elasticsearch玩出了怎样的新操作?
导语 | 腾讯会议于2019年12月底上线,两个月内日活突破1000万,被广泛应用于疫情防控会议.远程办公.师生远程授课等场景,为疫情期间的复工复产提供了重要的远程沟通工具.上线100天内,腾讯会议快 ...
- vue学习(六) 事件修饰符 stop prevent capture self once
//html <div id="app"> <div @click="divHandler" style="height:150px ...
- 《闲扯Redis七》Redis字典结构的底层实现
一.前言 上节<闲扯Redis六>Redis五种数据类型之Hash型 中说到 Hash(哈希对象)的底层实现有: 1.ziplist 编码的哈希对象使用压缩列表作为底层实现 2.hasht ...
- IDEA中搭建项目环境
ladies and gentlemen,Welcome to my blog! 本文主要在IDEA中搭建项目环境. 有问题和指正,欢迎下方留言~ 1. 使用GitLab将项目下载下来 1.1 选 ...