[leetcode349]Intersection of Two Arrays

Posted stAr_1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[leetcode349]Intersection of Two Arrays相关的知识,希望对你有一定的参考价值。

设计的很烂的一道题

 List<Integer> res = new ArrayList<>();
//        int l1 = nums1.length;
//        int l2 = nums2.length;
//        int l = 0;
//        while (l<l1&&l<l2)
//        {
//            if (nums1[l]==nums2[l])
//                res.add(nums1[l]);
//            l++;
//        }
//        int[] nums = new int[res.size()];
//        for (int i = 0; i < nums.length; i++) {
//            nums[i] = res.get(i);
//        }
//        return nums;
        Set<Integer> set = new HashSet<>();
        for (int a :
                nums1) {
            set.add(a);
        }
        for (int i = 0; i < nums2.length; i++) {
            if (set.contains(nums2[i]))
                res.add(nums2[i]);
        }
        set = new HashSet<>(res);
        int[] nums = new int[set.size()];
        int i =0;
        for (int a :
                set) {
            nums[i] = a;
            i++;
        }
        return nums;

 

以上是关于[leetcode349]Intersection of Two Arrays的主要内容,如果未能解决你的问题,请参考以下文章

[leetcode]349.Intersection of Two Arrays

Leetcode-349 Intersection of Two Arrays

Leetcode刷题记录[python]——349 Intersection of Two Arrays

LeetCode(349)Intersection of Two Arrays

LeetCode_349. Intersection of Two Arrays

leetcode349. Intersection of Two Arrays