LeetCode - 349. Intersection of Two Arrays

这个题目中既然提到了distinct,duplicate相关的问题,那么自然可以想到使用Set。首先使用一个Set记录nums1中的所有元素,因为是Set,所以这样就消除了nums1中的重复元素,接着使用另一个Set记录nums1和nums2的共有元素,对于nums2中的元素,如果第一个Set中含有这个元素,那么将这个元素加入到第二个Set中。同样地,由于Set不允许重复元素,这样同样又消去了重复的元素,时间复杂度为O(n),代码如下:

public class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> set = new HashSet<Integer>();
        Set<Integer> intersect = new HashSet<Integer>();
        
        for (int i = 0; i < nums1.length; i++) {
            set.add(nums1[i]);
        }
        for (int i = 0; i < nums2.length; i++) {
            if (set.contains(nums2[i])) {
                intersect.add(nums2[i]);
            }
        }
        
        // Construct result
        int[] result = new int[intersect.size()];
        int i = 0;
        for (Integer num : intersect) {
            result[i++] = num;
        }
        return result;
    }
}


第二种方法是使用两个指针和一个Set,首先初始化两个指针i和j分别指向nums1和nums2的开头,如果nums1[i]比较小的话,i++;如果nums[j]比较小的话,就j++;如果两个相等,就将它们指向的元素加入Set当中,并且i++, j++。最后使用Set中的元素construct result,注意使用这种方法需要首先对两个数组进行排序,时间复杂度为O(nlogn),代码如下:

public class Solution{
    public int[] intersect(int[] nums1, int[] nums2){
        Set<Integer> set = new HashSet<>();
        
        Arrays.sort(nums1);
        Arrays.sort(nums2);

        int i = 0;
        int j = 0;
        while(i < nums1.length && j < nums2.length){
            if(nums1[i] < nums2[j]){
                i++;
            }else if(nums1[i] > nums1[j]){
                j++;
            }else{
                set.add(nums1[i]);
                i++;
                j++;
            }
        }

        // Construct result
        int[] result = new int[set.size()];
        int k = 0;
        for(int num : set){
            result[k++] = num;
        }
        return result;
    }
}


第三中方法是使用binary search和一个Set,对于nums1中的某个元素,使用binary search在nums2中寻找这个元素,找到的话就加入到Set中,最后construct result。注意这种方法也要先对nums2进行排序,时间复杂度为O(nlogn),代码如下:

public class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> set = new HashSet<>();
        Arrays.sort(nums2);
        for (Integer num : nums1) {
            if (binarySearch(nums2, num)) {
                set.add(num);
            }
        }
        int i = 0;
        int[] result = new int[set.size()];
        for (Integer num : set) {
            result[i++] = num;
        }
        return result;
    }
    
    public boolean binarySearch(int[] nums, int target) {
        int low = 0;
        int high = nums.length - 1;
        while (low <= high) {
            int mid = low + (high - low) / 2;
            if (nums[mid] == target) {
                return true;
            }
            if (nums[mid] > target) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        return false;
    }
}


知识点:

1. Java中的Set是一个interface,不能直接进行实例化,用的时候要实例化实现了这个interface的class,比如使用HashSet来初始化

2. Set的使用方法,对于一堆元素,可以直接使用set.add(element),如果有重复的话,add不会成功,同样也不会出问题;Set.size()

3. Set可以被迭代使用,也就是for(int element : Set)这样的用法,要学习使用这种方法

4. 使用Binary Search一定要首先进行排序,这点要牢记

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值