• 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.

    本题解法比较多,先来说一说我的解法:1.将两个数组进行排序,然后创建两个指针对这两个数组进行遍历,(假设指针为i,j),如果nums1[i]<nums2[j],那么i++;如果nums1[i]>nums2[j],那么j++;否则如果两个值相等,则将其值放入hashset里面,代码如下:

     1 public class Solution {
     2     public int[] intersection(int[] nums1, int[] nums2) {
     3         Set<Integer> set = new HashSet<Integer>();
     4         if(nums1==null||nums1.length==0||nums2==null||nums2.length==0) return new int[0];
     5         Arrays.sort(nums1);
     6         Arrays.sort(nums2);
     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                // System.out.println(nums1[i]);
    16                 set.add(nums1[i]);
    17                 i++;
    18                 j++;
    19             }
    20         }
    21         int[] res= new int[set.size()];
    22         int q = 0;
    23         for(int k:set){
    24             res[q++]=k;
    25         }
    26         return res;
    27     }
    28 }

    上述代码的时间复杂度为O(nlongn);

    第二种方法与之前的Repeated DNA Sequence比较类似,都是创建两个hashset,第一个来保存第一个数组的元素,然后检测是否第二个数组包括第一个数组里面元素,

    如果包括,则把它放入第二个hashset里面。他和DNAsequence有个不同之处在于,DNA的第二个hashset用来剔除掉第三次出现的情况;这种双hashset的问题可以用来解决字符重复性的问题,代码如下:

     1 public class Solution {
     2     public int[] intersection(int[] nums1, int[] nums2) {
     3         Set<Integer> set = new HashSet<Integer>();
     4         Set<Integer> res = new HashSet<Integer>();
     5         for(int i:nums1){
     6             set.add(i);
     7         }
     8         for(int i:nums2){
     9             if(set.contains(i)){
    10                 res.add(i);
    11             }
    12         }
    13         int[] result = new int[res.size()];
    14         int k =0;
    15         for(Integer i:res){
    16             result[k++] = i;
    17         }
    18         return result;
    19     }
    20 }
    21 // run time complexity is O(max(nums1.length,nums2.length)) space complexity O(min(nums1.length,nums2.length))

    上述方法的时间复杂度是O(n);

    第三种方法就是使用二分查找法,对其中的一个数组进行排序,遍历另外一个数组,对另外一个数组的每个num都进行查找,如果找到了,则说明这两个数组都含有这个值,将其放入hashset里面,否则则不包含,代码如下:

     1 public class Solution {
     2     public int[] intersection(int[] nums1, int[] nums2) {
     3         Set<Integer> set = new HashSet<Integer>();
     4         Arrays.sort(nums2);
     5         for(int i:nums1){
     6             if(binarySearch(nums2,i)){
     7                 set.add(i);
     8             }
     9         }
    10         int[] res = new int[set.size()];
    11         int v = 0;
    12         for(int i:set){
    13             res[v++] = i;
    14         }
    15         return res;
    16     }
    17     public boolean binarySearch(int[] nums,int target){
    18         int i=0;
    19         int j = nums.length-1;
    20         while(i<=j){
    21             int mid = i+(j-i)/2;
    22             if(nums[mid]==target) return true;
    23             else if(nums[mid]<target) i = mid+1;
    24             else j = mid-1;
    25         }
    26         return false;
    27     }
    28 }
  • 相关阅读:
    MIP技术进展月报第3期:MIP小姐姐听说,你想改改MIP官网?
    MIP技术进展月报第2期: 数据绑定,异步脚本加速
    WebP 在减少图片体积和流量上的效果如何?MIP技术实践分享
    改造MIP获得搜索青睐,轻松完成SEO
    MIP 技术进展月报:储存功能全新上线,MIP-Cache域名升级,校验更严谨
    【转】W3C中国与百度联合组织移动网页加速技术研讨会
    百度将与W3C中国召开MIP技术研讨会
    【公告】MIP组件审核平台故障-影响说明
    【公告】关于8.8MIP组件审核平台故障的说明
    MIP 移动网页加速器视频教程全新发布
  • 原文地址:https://www.cnblogs.com/codeskiller/p/6592558.html
Copyright © 2020-2023  润新知