16.<tag-数组和排序, 双指针, 哈希表>-lt.349-两个数组的交集 + lt.350-两个数组的交集 || 1

lt.349-两个数组的交集

[案例需求]
在这里插入图片描述
[思路分析一, 使用哈希表]

  • 由题, 题目要求返回的数不重复, 所以我们使用两个HashSet存储两个数组,
  • 对第一个数组, 我们用第一个hashset存储所有的元素,
  • 对于第二个数组, 我们通过与第一个数组比较, 把相同的元素存入第二个hashset
  • 然后再把hashset转为数组即可.

在这里插入图片描述

class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> set1 = new HashSet<>();
        Set<Integer> set2 = new HashSet<>();

        for(int i : nums1){
            set1.add(i);
        }

        for(int j : nums2){
            if(set1.contains(j)){
                set2.add(j);
            }
        }

        int[] res = new int[set2.size()];
        int j = 0;

        for(int x : set2){
            res[j++] = x;
        }

        return res;
    }
}

在这里插入图片描述

[思路分析二, 排序和双指针]

如果两个数组是有序的,则可以使用双指针的方法得到两个数组的交集。

首先对两个数组进行排序,然后使用两个指针遍历两个数组。可以预见的是加入答案的数组的元素一定是递增的,为了保证加入元素的唯一性,我们需要额外记录变量 pre表示上一次加入答案数组的元素。

初始时,两个指针分别指向两个数组的头部。每次比较两个指针指向的两个数组中的数字,如果两个数字不相等,则将指向较小数字的指针右移一位,如果两个数字相等,且该数字不等于 pre ,将该数字添加到答案并更新 pre 变量,同时将两个指针都右移一位。当至少有一个指针超出数组范围时,遍历结束。

class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        //排序+双指针
        // 双指针一般用在有序的数组中, 所以我们先对两个数组进行排序
        Arrays.sort(nums1);
        Arrays.sort(nums2);

        int left = 0, right = 0;
        int[] intersection = new int[nums1.length + nums2.length];
        
        int index = 0;

        while(left < nums1.length && right < nums2.length){
            /**
                双指针的移动规则
                1. 小于的指针先移动, left < right left移动, right不动, 反之亦然
                2. =的两个指针一起移动.
             */
            if(nums1[left] < nums2[right]){
                ++left;
            }else if(nums1[left] > nums2[right]){
                ++right;
            }else{

                if(index == 0 || nums1[left] != intersection[index - 1]){
                        intersection[index++] = nums1[left];
                }
                
                ++left;
                ++right;
            }
        }

        return Arrays.copyOfRange(intersection, 0, index);
    }
}

lt.350-两个数组的交集 ||

[案例需求]
在这里插入图片描述

[思路分析]

[代码实现]

class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        //排序+双指针
        // 双指针一般用在有序的数组中, 所以我们先对两个数组进行排序
        Arrays.sort(nums1);
        Arrays.sort(nums2);

        int left = 0, right = 0;
        int[] intersection = new int[nums1.length + nums2.length];
        
        int index = 0;

        while(left < nums1.length && right < nums2.length){
            /**
                双指针的移动规则
                1. 小于的指针先移动, left < right left移动, right不动, 反之亦然
                2. =的两个指针一起移动.
             */
            if(nums1[left] < nums2[right]){
                ++left;
            }else if(nums1[left] > nums2[right]){
                ++right;
            }else{

           
                        intersection[index++] = nums1[left];
                
                
                ++left;
                ++right;
                }
            }
             return Arrays.copyOfRange(intersection, 0, index);
    }

       
}

对进阶的解答:

在这里插入图片描述
点我

posted @   青松城  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示