两数组的交集(无重复)Intersection of Two Arrays
2019独角兽企业重金招聘Python工程师标准>>>
问题:
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1]
, nums2 = [2, 2]
, return [2]
.
Note:
- Each element in the result must be unique.
- The result can be in any order.
解决:
①使用hashSet保存nums1数组中的值,然后比较即可。耗时5ms.
public class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
List<Integer> list = new ArrayList<>();
Set<Integer> set = new HashSet<>();
for (int n : nums1) {
set.add(n);
}
for (int n : nums2) {
if (set.contains(n)) {
list.add(n);
set.remove(n);
}
}
int res[] = new int[list.size()];
for(int i = 0;i < list.size();i ++ ){
res[i] = list.get(i);
}
return res;
}
}
②使用双指针法, 使用一个临时数组保存相同的数值,当扫描时存在与临时数组最后一个值相同的数时,跳过,否则,添加到临时数组中。
public class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
if (nums1 == null || nums2 == null || nums1.length == 0 || nums2.length == 0) return new int[0];
Arrays.sort(nums1);
Arrays.sort(nums2);
int temp[] = new int[nums1.length];
int p1 = 0;
int p2 = 0;
int p = 0;
while(p1 < nums1.length && p2 < nums2.length){
if (nums1[p1] == nums2[p2]) {
if (p - 1 >= 0 && temp[p - 1] == nums1[p1]) {//相同的值重复
//什么都不做
}else{
temp[p] = nums1[p1];
p ++;
}
p1 ++;
p2 ++;
}else if (nums1[p1] < nums2[p2]) {
p1 ++;
}else{
p2 ++;
}
}
int res[] = new int[p];
for (int i = 0;i < p ;i ++ ) {
res[i] = temp[i];
}
return res;
}
}
转载于:https://my.oschina.net/liyurong/blog/993827
两数组的交集(无重复)Intersection of Two Arrays相关推荐
- LeetCode算法题350:两个数组的交集II(Intersection of Two Arrays II)
技术交流可以加: 本人微信:xcg852390212 本人qq:852390212 学习交流qq群1(已满): 962535112 学习交流qq群2: 780902027 两个数组的交集II Leet ...
- C#LeetCode刷题之#350-两个数组的交集 II(Intersection of Two Arrays II)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4044 访问. 给定两个数组,编写一个函数来计算它们的交集. 输入 ...
- 53 两数组的交集(Intersection of Two Arrays)
文章目录 1 题目 2 解决方案 2.1 思路 2.3 时间复杂度 2.4 空间复杂度 3 源码 3.1 排序+合并的方式 3.2 二分搜索的方式 3.3 哈希表的方式 1 题目 题目:两数组的交集( ...
- C# 算法题系列(一) 两数之和、无重复字符的最长子串
题目一 原题链接 https://leetcode-cn.com/problems/two-sum/ 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整 ...
- 2022-6-13 全O(1)的数据结构,两数相加,无重复字符的最长子串,寻找两个正序数组的中位数,盛最多水的容器,......
1. 全 O(1) 的数据结构 Design a data structure to store the strings' count with the ability to return the s ...
- 如何用C#求两数组的交集和并集
string[] a1={"1","2"}; string[] a2={"2","3"}; Ilist<strin ...
- php 两个数组求交集_如何求两个数组的交集
题目描述 给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入:nums1 = [1,2,2,1], nums2 = [2,2]输出:[2] 示例 2: 输入:nums1 = [4,9,5] ...
- Leetcode-数据结构-350. 两个数组的交集 II
问题 给两个整数数组 nums1 和 nums2 ,请以 数组形式 返回两数组的 交集 (其在交集中出现的次数:等于该数字在两个数组中出现次数的最小值). 返回结果中 每个元素出现的次数(for遍历) ...
- Leetcode刷题100天—349. 两个数组的交集(集合)—day08
前言: 作者:神的孩子在歌唱 大家好,我叫运智 349. 两个数组的交集 难度简单410收藏分享切换为英文接收动态反馈 给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入:nums1 = ...
最新文章
- Android客户端开发—数据库SQLite基本语句
- 使用Python和OpenCV进行文本偏斜校正
- 10 个神奇的网站显示超强的 HTML5 技术
- 高级政工师具备的能力_一个合格的政工师应该具备哪些条件与素质
- 零积分下载,2014年辛星mysql教程秋季版第一本已经完工,期待您的支持
- python用途与前景-Python 就业方面的选择与应用分析
- 1_发表论文1_题目:(20181127)
- JVM异常之:方法区溢出OutOfMemoryError: PermGen space
- boost::detail::sp_convertible相关的测试程序
- python和按键精灵自动化测试_按键精灵对APP自动化测试(下)
- hbase数据导入到mysql(转载+自己验证整理,目前失败)
- 优先队列priority_queue 用法详解
- SetConsoleCtrlHandler() -- 设置控制台信号处理函数
- React—Native开发之 Could not connect to development server(Android)解决方法
- matlab异步电动机转速,转速闭环恒压频比异步电机调速系统 MatlabSimulink 仿真.pdf...
- 《蔡康永的说话之道》书摘
- cannot find zipfile directory
- [转载]分布式双活数据中心
- 都说数据是资产,那么到底什么是数据资产?
- shell脚本批量处理ping IP测试