首页 > 编程语言 > 详细

[leetcode350]Intersection of Two Arrays II求数组交集

时间:2018-01-21 17:08:21      阅读:199      评论:0      收藏:0      [点我收藏+]
List<Integer> res = new ArrayList<>();
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        int i1 = 0;
        int i2 = 0;
        while (i1<nums1.length&&i2<nums2.length)
        {
            if (nums1[i1]<nums2[i2])
                i1++;
            else if (nums1[i1]>nums2[i2])
                i2++;
            else
            {
                res.add(nums1[i1]);
                i1++;
                i2++;
            }
        }
        int[] num = new int[res.size()];
        for (int i = 0; i < num.length; i++) {
            num[i] = res.get(i);
        }
        return num;

受上一题的影响,本来想用hashset解决,但是发现不行,就换了排序然后遍历的方法,如果不相等,小的数的下边++,相等就添加

[leetcode350]Intersection of Two Arrays II求数组交集

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

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