• LC.349. Intersection of Two Arrays


    https://leetcode.com/problems/intersection-of-two-arrays/description/
    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.


    corner case, ways to initialize array
    1: int[] res = new int[x] ;
    2: int[] res = new int[]{1,2,3} ;
    3: declare but initialize later
    int[] res ;
    res = new int[3] ;


     1 /**
     2      * if using binary search:
     3      * time:   nums1 #: n   nums2 #: m
     4      * sorting for num2: mlogm
     5      * b.s for num1: n*log(m)
     6      * total mlogm + n*log(m)  ---> nlogn
     7      *
     8      * space: o(n) for using hashset storing nums1
     9      */
    10     public int[] intersection_bs(int[] nums1, int[] nums2) {
    11         if (nums1 == null || nums1.length == 0 || nums2 == null || nums2.length == 0) {
    12             return new int[]{};
    13         }
    14         Set<Integer> set = new HashSet<>();
    15         Arrays.sort(nums2);
    16         for (Integer num : nums1) {
    17             if (binarySearch(nums2, num)) {
    18                 set.add(num);
    19             }
    20         }
    21         //convert from set to to array
    22         int i = 0;
    23         int[] resArray = new int[set.size()];
    24         for (Integer num : set) {
    25             resArray[i++] = num;
    26         }
    27         return resArray;
    28     }
    29 
    30     private boolean binarySearch(int[] nums2, Integer num) {
    31         int left = 0, right = nums2.length - 1;
    32         while (left + 1 < right) {
    33             int mid = left + (right - left) / 2;
    34             if (nums2[mid] == num) {
    35                 return true;
    36             } else if (nums2[mid] < num) {
    37                 left = mid;
    38             } else {
    39                 right = mid;
    40             }
    41         }
    42         // post processing for the left and right
    43         return nums2[left] == num || nums2[right] == num;
    44     }
    45 
    46     //use two pointers time: o(n) space: o(n)
    47     public int[] intersection_twoPointer(int[] nums1, int[] nums2) {
    48         if (nums1 == null || nums1.length == 0 || nums2 == null || nums2.length == 0) {
    49             return new int[]{};
    50         }
    51         Set<Integer> set = new HashSet<>();
    52         Arrays.sort(nums1);
    53         Arrays.sort(nums2);
    54         int i = 0;
    55         int j = 0;
    56         //here time complexity is: n + m: 取交集,当然要两个都不为空
    57         while (i < nums1.length && j < nums2.length) {
    58             if (nums1[i] < nums2[j]) {
    59                 i++;
    60             } else if (nums1[i] > nums2[j]) {
    61                 j++;
    62             } else {
    63                 set.add(nums1[i]);
    64                 i++;
    65                 j++;
    66             }
    67         }
    68         //convert from set to to array
    69         int i = 0;
    70         int[] resArray = new int[set.size()];
    71         for (Integer num : set) {
    72             resArray[i++] = num;
    73         }
    74         return resArray;
    75     }
    76 
    77     //method 3: time o(n) space: o(n)
    78     public int[] intersection_twoSets(int[] nums1, int[] nums2) {
    79         if (nums1 == null || nums1.length == 0 || nums2 == null || nums2.length == 0) {
    80             return new int[]{};
    81         }
    82         Set<Integer> set = new HashSet<>();
    83         for (Integer num : nums1) {
    84             //when you add nums to set, dont need to consider dup. will overwrite
    85             set.add(num);
    86         }
    87         Set<Integer> ret = new HashSet<>();
    88         for (Integer num : nums2) {
    89             if (set.contains(num)) {
    90                 ret.add(num);
    91             }
    92         }
    93         int[] resArr = new int[ret.size()];
    94         int i = 0;
    95         for (Integer num : ret) {
    96             resArr[i++] = num;
    97         }
    98         return resArr;
    99     }

     

  • 相关阅读:
    elementui问题汇总
    微信小程序实现微信授权登录
    微信小程序数据存储
    小程序使用第三方服务,需要中转到一个h5页面,返回到指定页面
    小程序开发,通过左上角返回到指定页面
    万恶之源-基本数据类型(list,tuple)
    基础中的基础
    mybatis_plus实现自动填充和逻辑删除
    本地端口占用解决方案
    maven定义版本以来报红解决方案
  • 原文地址:https://www.cnblogs.com/davidnyc/p/8471308.html
Copyright © 2020-2023  润新知