来源:https://leetcode.com/problems/intersection-of-two-arrays

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.

1. 直接使用HashSet

 class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> set = new HashSet<>();
Set<Integer> intersect = new HashSet<>();
for(int i=0; i<nums1.length; i++) {
set.add(nums1[i]);
}
for(int i=0; i<nums2.length; i++) {
if(set.contains(nums2[i])) {
intersect.add(nums2[i]);
}
}
int[] result = new int[intersect.size()];
int i = 0;
for(Integer num: intersect) {
result[i++] = num;
}
return result;
}
}// 6 ms

2. 类似BitMap的思想,LeetCode 1ms sample,缺点是数组中的元素不能为负数……

(1)求出两个数组的最大值

(2)建立 长度=最大值+1 的数组A,假设数组的索引为i,那么A[i]的值表示i是否在两个数组中存在,若为0表示不存在于任何一个数组中,为1表示存在于第一个数组中,为2表示两个数组中都存在

 class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
int max = 0;
for(int num: nums1) {
if(num > max) {
max = num;
}
}
for(int num: nums2) {
if(num > max) {
max = num;
}
}
int[] indexMap = new int[max+1];
for(int num: nums1) {
indexMap[num] = 1;
}
int cnt = 0;
for(int num: nums2) {
if(indexMap[num] == 1) {
indexMap[num] = 2;
cnt++;
}
}
int[] result = new int[cnt];
for(int i=0; i<max+1; i++) {
if(indexMap[i] == 2) {
result[--cnt] = i;
}
}
return result;
}
}

Intersection of Two Arrays(交集)的更多相关文章

  1. [LeetCode] Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  2. Python 解LeetCode:Intersection of Two Arrays

    最近,在用解决LeetCode问题的时候,做了349: Intersection of Two Arrays这个问题,就是求两个列表的交集.我这种弱鸡,第一种想法是把问题解决,而不是分析复杂度,于是写 ...

  3. 【一天一道LeetCode】#350. Intersection of Two Arrays II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  4. [LeetCode] 349. Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...

  5. [LeetCode] 350. Intersection of Two Arrays II 两个数组相交II

    Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...

  6. [LeetCode] Intersection of Two Arrays II 两个数组相交之二

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  7. LeetCode Intersection of Two Arrays

    原题链接在这里:https://leetcode.com/problems/intersection-of-two-arrays/ 题目: Given two arrays, write a func ...

  8. LeetCode Intersection of Two Arrays II

    原题链接在这里:https://leetcode.com/problems/intersection-of-two-arrays-ii/ 题目: Given two arrays, write a f ...

  9. [LintCode] Intersection of Two Arrays II 两个数组相交之二

    Given two arrays, write a function to compute their intersection.Notice Each element in the result s ...

  10. [LintCode] Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection.Notice Each element in the result m ...

随机推荐

  1. redis分布式锁原理与实现

    分布式锁原理 分布式锁,是控制分布式系统之间同步访问共享资源的一种方式.在分布式系统中,常常需要协调他们的动作.如果不同的系统或是同一个系统的不同主机之间共享了一个或一组资源,那么访问这些资源的时候, ...

  2. ZROI 19.08.04模拟赛

    传送门 写在前面:为了保护正睿题目版权,这里不放题面,只写题解. "这应该是正睿OI历史上第一次差评破百的比赛." "这说明来正睿集训的人越来越多了." &qu ...

  3. 2018微信小程序官方示例源码最新版

    忘记从哪获得的 CSDN  可以支持一下 谢谢你们 https://download.csdn.net/download/lan1128/10197682 当然也有免费的 代码在码云上免费公开 点个关 ...

  4. shell练习--关于关联数组自增统计判断的学习

    今天在书上看到了一个关联数组 let statarray["$ftype"]++  这样一个操作,用来做索引的自增统计,所以记下来 #!/bin/bash #统计文件类型 #关于关 ...

  5. 第九周作业—N42-虚怀若谷

    一.编写脚本,接收二个位置参数,magedu和/www,判断系统是否有magedu,如果没有则自动创建magedu用户,并自动设置家目录为/www [root@centos7 data]# cat u ...

  6. Let Us Adore 让我们来敬拜祂 中文歌词

      Verse 1 诸天宣告 神的荣耀 万国万民 都将赞美 宣扬祂奇妙 The heavens declare The glory of God And all of the world Will j ...

  7. jquery header选择器 语法

    jquery header选择器 语法 作用::header 选择器选取所有标题元素(h1 - h6).广州大理石机械构件 语法:$(":header") jquery heade ...

  8. EventArgs

    序言 DataEventArgs<DataSet> arg = new DataEventArgs<DataSet>(ds); 事件总线 什么是事件总线 我们知道事件是由一个P ...

  9. MongoDB操作:update()

    @Override public boolean update(String dbName, String collectionName, DBObject oldValue, DBObject ne ...

  10. golang rabbitmq实践 (一 rabbitmq配置)

    1:环境选择 系统为ubuntu 15.04 ,我装在虚拟机里面的 2:rabbitmq tabbitmq 3.5.4  download url : http://www.rabbitmq.com/ ...