首页 > 其他 > 详细

[leetcode349]Intersection of Two Arrays

时间:2018-01-21 16:59:21      阅读:119      评论:0      收藏:0      [点我收藏+]

设计的很烂的一道题

 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

原文:https://www.cnblogs.com/stAr-1/p/8324513.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!