Problem:

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

中文:已知两个数组,写一个函数来计算它们的交集

Example:

  • Given nums1 = [1, 2, 2, 1], nums2 = [2, 2],return [2, 2].


  • 已知nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].

Note:

  • Each element in the result should appear as many times as it shows in both arrays.
  • The result can be in any order.


  • (每个元素出现的次数与原来两个数组中重复的次数相同)

  • (数组的元素可以是任意顺序的)

Follow up:

  • What if the given array is already sorted? How would you optimize your algorithm?
  • What if nums1’s size is small compared to num2’s size? Which algorithm is better?
  • What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?

  • 假如已知的数组是有序的该如何优化算法?
  • 假如数组1的大小比数组2要小该如何优化?
  • 假如数组2的元素在磁盘上是排序好的,但是内存不能一次性容纳这些元素,该怎么做?

Solution:

Analysis:

  • 1.直接的想法:直接嵌套遍历两个数组,遇到相同元素的就加入一个预定义好的数组中。

    • 预定义的数组长度是数组1和数组2中较小的一个。
    • 最后将该数组有效的元素重新移植到新的一个数组中。
  • 2.修正1:得到的数组可能有很多重复的元素。
    • 要再添加新元素到数组中时检查数组中是否已经存在该元素。
    • 数组会越界,所以要在添加前检查元素个数是否超过了数组长度。

Code in JAVA

public static int[] intersection(int[] a1, int[] a2) {
int n = Math.min(a1.length, a2.length);
int[] is = new int[n];
int count = 0;
for(int i = 0; i < a1.length; i++){
int tmep = a1[i];
for(int j = 0; j < a2.length; j++){
if(tmep == a2[j]){
boolean exist = false;
for(int k = 0; k < count; k++){
if(is[k] == tmep){
exist = true;
break;
}
}
if(count >= n){
break;
}
if(!exist){
is[count] = tmep;
count++;
} break;
}
}
}
int[] itersection = new int[count];
for(int i = 0; i < count; i++){
itersection[i] = is[i];
} return itersection;
}

leetcode 349:两个数组的交集I的更多相关文章

  1. Java实现 LeetCode 349 两个数组的交集

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

  2. Leetcode 349. 两个数组的交集 By Python

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

  3. 前端与算法 leetcode 350. 两个数组的交集 II

    目录 # 前端与算法 leetcode 350. 两个数组的交集 II 题目描述 概要 提示 解析 解法一:哈希表 解法二:双指针 解法三:暴力法 算法 # 前端与算法 leetcode 350. 两 ...

  4. Java实现 LeetCode 350 两个数组的交集 II(二)

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

  5. leetcode NO.349 两个数组的交集 (python实现)

    来源 https://leetcode-cn.com/problems/intersection-of-two-arrays/ 题目描述 给定两个数组,写一个函数来计算它们的交集. 例子: 给定 nu ...

  6. Leetcode 350.两个数组的交集|| By Python

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

  7. python(leetcode)-350两个数组的交集

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

  8. 领扣(LeetCode)两个数组的交集II 个人题解

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

  9. LeetCode 350: 两个数组的交集 II Intersection of Two Arrays II

    题目: 给定两个数组,编写一个函数来计算它们的交集. Given two arrays, write a function to compute their intersection. 示例 1: 输 ...

  10. [LeetCode] 350. 两个数组的交集 II intersection-of-two-arrays-ii(排序)

    思路: 先找到set的交集,然后分别计算交集中的每个元素在两个原始数组中出现的最小次数. class Solution(object): def intersect(self, nums1, nums ...

随机推荐

  1. Hibernate3回顾-1-部署

    web备份版本,详见doc版本. 一.背景(部署简单回顾) 我们知道,一个Hibernate快速上手的简单流程是这样. 1引入对应jar包. 中间涉及log4的jar包和配置,略. 2 实体类 pac ...

  2. dismissViewControllerAnimated有延迟

    dismissViewControllerAnimated:completion:在应用中运行正常,就是不知道为什么出现了几秒钟的延迟: [api loginWithUsername:[dict ob ...

  3. discuz!3 二次开发C#学者

    PHP入门,从搞数据库开始: 大致看了以下PHP还是很简单的 比如链接数据库,就这么几行,比asp.net简单的多,就是需要自己搞数据的显示,需要精通HTML和代码生成技术: <?php $co ...

  4. SPOJ #442 Searching the Graph

    Just CS rookie practice on DFS\BFS. But details should be taken care of: 1. Ruby implementation got ...

  5. 读书笔记:Sheldon Ross:概率论基础教程:随机变量

    例1b 一个坛子里装有编号1-20的球,无放回抽取3个,取出球中至少一个号码大于等于17的概率是多少? 除了书上的解法外,还有一种解法: 考虑相反的情况:三个球的号码都小于17. 第一次从编号1-16 ...

  6. 8张图带你深入理解Java

    1.字符串的不变性 下图展示了如下的代码运行过程: String s = "abcd";s = s.concat("ef");   备注:String refe ...

  7. bzoj3905: Square

    Description Nothing is more beautiful than square! So, given a grid of cells, each cell being black ...

  8. erlang的shell历史记录

    erlang的shell默认重启以后木有历史记录,略蛋疼,开发的时候略不便 网上找了个方式 sudo apt-get install rlwrap vim ~/.bash_profile alias ...

  9. java定时器和多线程实践记录

    这几天因为需要测试mongodb读写分离的问题,因此写了个定时查询程序,并且用到了多线程,以达到定时启动多个线程查询数据库的效果,下边代码记录备忘: package timmer; import ja ...

  10. TSP(旅行者问题)——动态规划详解(转)

    1.问题定义 TSP问题(旅行商问题)是指旅行家要旅行n个城市,要求各个城市经历且仅经历一次然后回到出发城市,并要求所走的路程最短. 假设现在有四个城市,0,1,2,3,他们之间的代价如图一,可以存成 ...