349. Intersection of Two Arrays【双指针|二分】

2017/3/23 15:41:47

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

Example:
Given nums1 = [1, 2, 2, 1]nums2 = [2, 2], return [2].

Note:

  • Each element in the result must be unique.
  • The result can be in any order.

作弊版:Python

  1. classSolution(object):
  2. def intersection(self, nums1, nums2):
  3. return list(set( nums1 )& set(nums2))

版本1:Java  O(m*n)  循环检查

  1. publicclassSolution{
  2. publicint[] intersection(int[] nums1,int[] nums2){
  3. Set<Integer> set =newHashSet<Integer>();
  4. for(int i=0;i<nums1.length;i++)
  5. for(int j=0;j<nums2.length;j++)
  6. if( nums1[i]== nums2[j]){
  7. set.add(nums1[i]);
  8. break;
  9. }
  10. Object[] obj = set.toArray();
  11. int[] rs =newint[obj.length];
  12. for(int i=0;i<obj.length;i++)
  13. rs[i]=(int)obj[i];
  14. return rs;
  15. }
  16. }

版本2:Java  O(m+n)  借助哈希表+Set,或者双Set

  1. publicint[] intersection(int[] nums1,int[] nums2){
  2. Map<Integer,Boolean> map =newHashtable<Integer,Boolean>();
  3. Set<Integer> set =newTreeSet<Integer>();
  4. for(int i=0;i<nums1.length;i++)
  5. map.put( nums1[i],true);
  6. for(int j=0;j<nums2.length;j++){
  7. if(map.get(nums2[j])==null)continue;
  8. set.add(nums2[j]);
  9. }
  10. int i =0;
  11. int[] rs =newint[set.size()];
  12. for(Integer num : set )
  13. rs[i++]= num;
  14. return rs;
  15. }
时间: 2024-08-05 09:29:15

349. Intersection of Two Arrays【双指针|二分】的相关文章

[LeetCode] 349 Intersection of Two Arrays &amp; 350 Intersection of Two Arrays II

这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/ 350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 题目&解法

Leetcode刷题记录[python]——349 Intersection of Two Arrays

一.前言 做了两题才慢慢摸清了leetcode的操作. 二.题349 Intersection of Two Arrays Given two arrays, write a function to compute their intersection. class Solution(object): def intersection(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int]

[leetcode] 349. Intersection of Two Arrays 解题报告

Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. Note: Each element in the result must be unique. The result can be in any order. 第一想法用HashMap<Integer, Boolean>,但错误,用两个

【LeetCode】 349. Intersection of Two Arrays 解题小结

题目: Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. 第一次做的时候先将nums1存储在一个unordered_map里,然后对nums2的元素判断是否在unordered_map中,如果在,放入新的集合中,然后遍历集合,把结果输出,这样运算次数就是3N了:其实可以在第二次循环的时候判

[LeetCode] NO. 349 Intersection of Two Arrays

[题目] Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. [题目解析] 这是一个很简单的求交集的问题,可以用Set来解决.如下. public static int[] intersection(int[] nums1, int[] nums2){ Set<Integer> set =

349. Intersection of Two Arrays java solutions

Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. Note: Each element in the result must be unique. The result can be in any order. Subscribe to see which companies asked

【leetcode】349. Intersection of Two Arrays

Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. Note: Each element in the result must be unique. The result can be in any order. 我们可以使用两个指针来做,先给两个数组排序,然后用两个指针分别指向两个数组的

349. Intersection of Two Arrays (Easy)

Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. Note: Each element in the result must be unique. The result can be in any order. 题意:保证列表中每个元素都是唯一的:利用python中列表求交集的方法: 思路

【一天一道LeetCode】#349. Intersection of Two Arrays

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. Note: Each element in the