首页 > 其他 > 详细

349. Intersection of Two Arrays

时间:2016-07-13 09:13:16      阅读:234      评论:0      收藏:0      [点我收藏+]
    /*
     * 349. Intersection of Two Arrays
     * 2016-7-12 by Mingyang
     * I II都非常的简单,因为就是很简单,第一个题目用HashSet来做,去掉重复的加入unique的就好了
     * 第二个就只需要加入list,用两个指针来做
     */
     public int[] intersection(int[] nums1, int[] nums2) {
            Set<Integer> set = new HashSet<>();
            Set<Integer> intersect = new HashSet<>();
            for (int i = 0; i < nums1.length; i++) {
                set.add(nums1[i]);
            }
            for (int i = 0; i < nums2.length; i++) {
                if (set.contains(nums2[i])) {
                    intersect.add(nums2[i]);
                }
            }
            int[] result = new int[intersect.size()];
            int i = 0;
            for (Integer num : intersect) {
                result[i++] = num;
            }
            return result;
        }

 

349. Intersection of Two Arrays

原文:http://www.cnblogs.com/zmyvszk/p/5665620.html

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