两个数组的交集 (Intersection of Two Arrays)

 

思路:

// @Title: 两个数组的交集 (Intersection of Two Arrays)
// @Author: qisiii
// @Date: 2021-08-16 16:12:15
// @Runtime: 2 ms
// @Memory: 38.8 MB
// @comment: 
// @flag: 
class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        HashSet set=new HashSet<>();
        HashSet<Integer> result=new HashSet<Integer>();
        for (int i : nums1) {
            set.add(i);
        }
        for (int i : nums2) {
            if (set.contains(i)){
                result.add(i);
            }
        }
        int[] r=new int[result.size()];
        int count=0;
        for (Integer i : result) {
            r[count++]=i.intValue();
        }
        return r;
    }
}

思路:

// @Title: 两个数组的交集 (Intersection of Two Arrays)
// @Author: qisiii
// @Date: 2024-09-12 20:01:05
// @Runtime: 2 ms
// @Memory: 42.3 MB
// @comment: 
// @flag: 
class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        HashSet<Integer> set=new HashSet<>();
        for(int i:nums1){
            set.add(i);
        }
        HashSet<Integer> result=new HashSet<>();
        for(int i:nums2){
            if(set.contains(i)){
                result.add(i);
            }
        }
        int[] arr=new int[result.size()];
        int count=0;
        for(Integer i:result){
            arr[count++]=i;
        }
        return arr;
    }
}

思路:

// @Title: 两个数组的交集 (Intersection of Two Arrays)
// @Author: qisiii
// @Date: 2024-09-12 20:00:18
// @Runtime: 2 ms
// @Memory: 42.3 MB
// @comment: 
// @flag: 
class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        HashSet<Integer> set=new HashSet<>();
        for(int i:nums1){
            set.add(i);
        }
        HashSet<Integer> result=new HashSet<>();
        for(int i:nums2){
            if(set.contains(i)){
                result.add(i);
            }
        }
        int[] arr=new int[result.size()];
        int count=0;
        for(Integer i:new ArrayList<>(result)){
            arr[count++]=i;
        }
        return arr;
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18