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. Hub, bridge, switch, router, gateway的区别

    这些概念性的东西,其实,有的区别不是很大,有的区别很大. Hub 就是一个重复转发器,就是从一个port接受到数据后,就会原样的向其他的所有端口发送刚才收到的数据.个人理解为是工作在物理层的东西.但是 ...

  2. cocoapods的安装及注意事项

    cocoapods是运行在ruby环境下的,在ruby环境的 ,像cocoapods这样的开源项目时放在放在rubygems服务器上面的,但国内访问https://rubygems.org/ 的时候会 ...

  3. Nginx负载均衡和反向代理设置

    Nginx负载均衡: 格式: upstream 别名 {    #别名一般要有意义,能看出是做什么的 server ip:端口;    #要实现负载的服务器的ip.端口号}  例: upstream ...

  4. WINDOWS黑客基础(6):查看文件里面的导入表

    int main(void) { HANDLE hFile = CreateFile("D:\\Shipyard.exe", GENERIC_READ, FILE_SHARE_RE ...

  5. 【freemaker】之FreeMakerUtil工具类

    Freemaker生成文件常用工具类 public class FreemakerUtil { private static FreemakerUtil util; private static Co ...

  6. SVN Server配置详解 及备份

    SVN简介和工作原理 subversion(简称svn)是近几年崛起的版本管理软件,是cvs的接班人,目前绝大多数开源软件都使用svn作为代码版本管理软件.Subversion支持linux和wind ...

  7. Php检测文件编码方法

    <?php /** * 检测文件编码 * @param string $file 文件路径 * @return string|null 返回 编码名 或 null */ function det ...

  8. 《黄聪:手机移动站SEO优化教程》1、为什么要做手机移动端网站

    视频地址:http://v.youku.com/v_show/id_XNzE2Mzk3MjI4.html

  9. objective-c中自己创建的对象为什么不能调用release

    dealloc方法,本就不应该手动调用. 你自己创建的对象,看你代码怎么写的了.例子:NSString *str1 = [NSString stringWithString:@"abc&qu ...

  10. SOA_环境安装系列3_Oracle Weblogic安装和环境搭建(案例)

    2014-01-03 Created By BaoXinjian