• 350. Intersection of Two Arrays II


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

    Example:
    Given 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 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?

    本题和intersection of two arrays1比较类似,只不过允许出现重复的元素了,那么能否用之前的三种方法来解决这个呢?二分查找那个肯定不可以,因为不可以动态删除数组元素;数组排序应该是可以的;另外时间复杂度O(n)的那个方法也是可以的,先说时间复杂度O(n)这个,只要把hashset换成hashmap就可以了,代码如下:

     1 public class Solution {
     2     public int[] intersect(int[] nums1, int[] nums2) {
     3         Map<Integer,Integer> map = new HashMap<Integer,Integer>();
     4         List<Integer> res = new ArrayList<Integer>();
     5         for(int i:nums1){
     6             map.put(i,map.getOrDefault(i,0)+1);
     7         }
     8         for(int i:nums2){
     9             if(map.containsKey(i)&&map.get(i)>0){
    10                 //find the int that appears in nums1,this method avoid remove operation
    11                 map.put(i,map.get(i)-1);
    12                 res.add(i);
    13             }
    14         }
    15         int[] result = new int[res.size()];
    16         int k=0;
    17         for(Integer i:res){
    18             result[k++] = i;
    19         }
    20         return result;
    21     }
    22 }
    23 //the run time complexity costs O(max(m,n)),the space complexity O(min(m,n));

    数组排序代码如下:

     1 public class Solution {
     2     public int[] intersect(int[] nums1, int[] nums2) {
     3         Map<Integer,Integer> map = new HashMap<Integer,Integer>();
     4         Arrays.sort(nums1);
     5         Arrays.sort(nums2);
     6         int count=  0;
     7         int i=0;
     8         int j=0;
     9         while(i!=nums1.length&&j!=nums2.length){
    10             if(nums1[i]<nums2[j]){
    11                 i++;
    12             }else if(nums1[i]>nums2[j]){
    13                 j++;
    14             }else{
    15                 count++;
    16                 map.put(nums1[i],map.getOrDefault(nums1[i],0)+1);
    17                 i++;
    18                 j++;
    19             }
    20         }
    21         int[] res = new int[count];
    22         int v = 0;
    23         for(int key:map.keySet()){
    24             for(int k=0;k<map.get(key);k++){
    25                 res[v++] = key;
    26             }
    27         }
    28         return res;
    29     }
    30 }

     本题的follow up的第三个问题有点难度,要看链接:

    https://discuss.leetcode.com/topic/45992/solution-to-3rd-follow-up-question

  • 相关阅读:
    SQL Server 数据类型映射 (ADO.NET)
    微软SQLHelper.cs类 中文版
    在WinForm中使用Web Service来实现软件自动升级
    Winform开发框架之通用自动更新模块(转)
    C# winform 最小化到电脑右下角
    3层数据访问架构(部分)
    Castle ActiveRecord学习实践
    .Net下的 ORM框架介紹
    配置企业库5.0管理
    一个简洁通用的调用DLL函数的帮助类
  • 原文地址:https://www.cnblogs.com/codeskiller/p/6592571.html
Copyright © 2020-2023  润新知