作者: 负雪明烛
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 日 —— 又到周五了!

最新文章

  1. h1、h2、h3标签及strong标签对页面seo的影响
  2. Storm-166:Nimbus HA solution based on Zookeeper
  3. Kaiju: Fast and sensitive taxonomic classification for metagenomics
  4. 【笔记】DOM探索基础篇(二)
  5. $.extend,$.fn.extend,$.fn的区别
  6. 函数xdes_init
  7. 问题:关于坛友一个获取text内容并改变样式的实现
  8. Linux vps无法发送邮件
  9. xampp配置多端口访问
  10. flex 左边固定宽度,右边自适应
  11. iReport 5.6.0 PDF导出中文不显示问题 解决方案
  12. ES6躬行记(4)——模板字面量
  13. HDFS上创建文件、写入内容
  14. GPUImage中亮度调整的实现——GPUImageBrightnessFilter
  15. 详解MathType中如何更改公式颜色
  16. day34 python学习 守护进程,线程,互斥锁,信号量,生产者消费者模型,
  17. svn新增文件时自动给文件设置强制只读属性needs-lock
  18. HTML利用posotion属性定位 小技巧
  19. hdu 5210 delete 水题
  20. (转)EDM邮件制作规范完整版

热门文章

  1. 集群SGE作业调度系统
  2. dokuwiki使用随笔
  3. Django创建多对多表关系的三种方式
  4. 学习Java的第三天
  5. 日常Java测试 2021/11/14
  6. admire, admit
  7. 25. Linux下gdb调试
  8. Spark基础:(一)初识Spark
  9. 【leetcode】563. Binary Tree Tilt
  10. 单链表的模板类(C++)