作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


  • Difficulty: Easy

题目描述

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

Example 1:

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]

Example 2:

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [4,9]

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 nums2’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?

解题方法

Java排序+双指针

题目的意思是找出两个数组中相同的元素,这个题目前面题使用HashSet去除的重复元素,这个题里边直接用ArrayList存储相同元素即可。

所以可以参考前面的,创建一个Arraylist,然后对两个数组进行排序,这样才能比较,根据不同的比较结果采取不同的措施。

对了,双指针的方法节省了不少空间。高票答案用的HashMap,效率明显没有双指针高。

public class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
ArrayList<Integer> arraylist = new ArrayList<Integer>();
//一定写上<Integer>,否则没法自动拆包装包
Arrays.sort(nums1);
Arrays.sort(nums2);
int index1 = 0;
int index2 = 0;
while (index1 < nums1.length && index2 < nums2.length) {
if (nums1[index1] == nums2[index2]) {
arraylist.add(nums1[index1]);
index1++;
index2++;
} else if (nums1[index1] < nums2[index2]) {
index1++;
} else {
index2++;
}
}
int answer[] = new int[arraylist.size()];
for (int i = 0; i < arraylist.size(); i++) {
answer[i] = arraylist.get(i);
}
return answer;
}
}

AC: 3 ms 超过96.76%

Python排序+双指针

看到题目说了如果已经排序了会怎么样,这是一个很明显的需要排序的提示,告诉我们先排序。下面的操作就像merge两个有序链表差不多,分别从两个的起始位置判断是否相等即可。

需要注意的是题目要求的是结果中的出现次数等于两个数组交集部分的次数,所以当两个数组元素相等的时候需要把两个指针同时右移。

时间复杂度O(NlogN),空间复杂度O(1).

class Solution:
def intersect(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
nums1.sort()
nums2.sort()
l1, l2 = 0, 0
N1, N2 = len(nums1), len(nums2)
res = []
while l1 != N1 and l2 != N2:
if nums1[l1] == nums2[l2]:
res.append(nums1[l1])
l1 += 1
l2 += 1
elif nums1[l1] < nums2[l2]:
l1 += 1
else:
l2 += 1
return res

Python解法使用字典

使用字典对两个数组出现的数字进行统计,然后直接判断数字是否在另一个字典里出现过,把结果直接拼接上两个的最小次数个当前数字。

时间复杂度O(N),空间复杂度O(N).打败了98%的提交。

class Solution:
def intersect(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
count1 = collections.Counter(nums1)
count2 = collections.Counter(nums2)
res = []
for k, v in count1.items():
if k in count2:
res += [k] * min(v, count2[k])
return res

日期

2017 年 1 月 11 日
2018 年 11 月 6 日 —— 腰酸背痛要废了
2018 年 11 月 16 日 —— 又到周五了!

【LeetCode】350. Intersection of Two Arrays II 解题报告(Java & Python)的更多相关文章

  1. 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

  2. 26. leetcode 350. Intersection of Two Arrays II

    350. Intersection of Two Arrays II Given two arrays, write a function to compute their intersection. ...

  3. [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 ...

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

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

  5. Python [Leetcode 350]Intersection of Two Arrays II

    题目描述: Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, ...

  6. LeetCode 350. Intersection of Two Arrays II (两个数组的相交之二)

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

  7. LeetCode 350. Intersection of Two Arrays II

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

  8. 【LeetCode】963. Minimum Area Rectangle II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 线段长+线段中心+字典 日期 题目地址:https: ...

  9. 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...

随机推荐

  1. alpine切换源 安装慢 apk add很慢

    alpine切换源 安装慢 apk add很慢 阿里镜像 sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/reposit ...

  2. Python中类的各式方法介绍

    本文类的方法介绍包括类方法.属性方法.静态方法.修改属性方法等内置装饰器装饰的方法,以及类的一些特殊成员方法 1. 类的特殊成员方法 1.1 构造方法 # -*- coding:utf-8 -*- # ...

  3. js浮点运算的坑

    1,js浮点型小数点运算的问题. 这么简单的计算,js竟然算的是错的,究其原因,是因为js小数在内存存储方式的原因. 具体原因: JavaScript 里的数字是采用 IEEE 754 标准的 64 ...

  4. 4.Reverse Words in a String-Leetcode

    class Solution { public: void reverseWords(string &s) { vector<string> data; string word; ...

  5. 一站式Flink&Spark平台解决方案——StreamX

    大家好,我是独孤风.今天为大家推荐的是一个完全开源的项目StreamX.该项目的发起者Ben也是我的好朋友. ****什么是StreamX,StreamX 是Flink & Spark极速开发 ...

  6. Scala(五)【集合的高级使用】

    目录 一.集合属性 size length contains mkString 二.集合方法 drop.dropRight distinct:去重 head.last:获取第一个.最后一个元素 tai ...

  7. 安全相关,xss

    XSS XSS,即 Cross Site Script,中译是跨站脚本攻击:其原本缩写是 CSS,但为了和层叠样式表(Cascading Style Sheet)有所区分,因而在安全领域叫做 XSS. ...

  8. Ubantu nodejs卸载与二进制安装

    #apt-get 卸载 sudo apt-get remove --purge npm sudo apt-get remove --purge nodejs sudo apt-get remove - ...

  9. oracle 拆分字符串

    WITH t AS (SELECT '1-2-3-4' a FROM dual)SELECT Regexp_Substr(a, '[^-]+', 1, LEVEL) i FROM tCONNECT B ...

  10. 【Java基础】transient关键字

    1. transient的作用及使用方法 我们都知道一个对象只要实现了Serilizable接口,这个对象就可以被序列化,java的这种序列化模式为开发者提供了很多便利,我们可以不必关系具体序列化的过 ...