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. [dts]Device Tree机制

    转自:http://blog.csdn.net/machiner1/article/details/47805069 ------------------Based on linux 3.10.24 ...

  2. C#应用程序单进程检测

    以下程序经过VS2010测试通过: /// <summary> /// 应用程序的主入口点. /// </summary> [STAThread] static void Ma ...

  3. GL_GL系列 - 会计期间管理分析(案例)

    2014-07-07 Created By BaoXinjian

  4. 把vector中的string对象导入到字符指针数组中

    #include <iostream>#include <string>#include <vector>//#include <cctype>#inc ...

  5. Java多线程之后台线程

    将线程设置成后台线程Daemons 主线程结果后,后台线程将自动结果. package wzh.test; import java.util.concurrent.TimeUnit; class Si ...

  6. TableViewCell的分割线显示不全解决方法

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath ...

  7. [C语言](一)第一个Windows 32 API的窗口程序

    #include <windows.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain( ...

  8. .Net调用非托管代码数据类型不一致的问题

    什么是Net互操作?.Net不能直接操作非托管代码,这时就需要互操作了.   c#中调用非托管c++函数,此函数又包含指向某个结构的指针,譬如指向c#中的byte数组.对于这样的参数,考虑到非托管变量 ...

  9. windows service的作成

    http://jingyan.baidu.com/article/fa4125acb71a8628ac709226.html

  10. UILabel使用技巧

    UILabel的各种属性与方法的使用(转) #import "LabelTestViewController.h" @implementation LabelTestViewCon ...