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){
…    }
}
上一篇:leetcode-1752. 检查数组是否经排序和轮转得到


下一篇:leecode349. 两个数组的交集