其他分享
首页 > 其他分享> > 349. 两个数组的交集

349. 两个数组的交集

作者:互联网

https://leetcode-cn.com/problems/intersection-of-two-arrays/

初始化两个set因为输出的元素是不重复的,使用set把第一个数组中的元素弄进来,然后看第二个数组中有没有包含的元素,有的话,加到第二个set中,然后新初始化的一个数组,将第二个set遍历到数组中,然后返回这个数组

class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        if(nums1 == null || nums1.length == 0 || nums2 == null || nums2.length == 0)  return new int[0];
        Set<Integer> set1 = new HashSet<>();
        Set<Integer> set2 = new HashSet<>();
        for(int i : nums1){
            set1.add(i);
        }
        for(int i : nums2){
…    }
}

标签:set,交集,nums1,int,数组,new,349,nums2
来源: https://www.cnblogs.com/zhbeii/p/15810347.html