首页 > 其他 > 详细

350. Intersection of Two Arrays II

时间:2016-07-13 09:10:16      阅读:336      评论:0      收藏:0      [点我收藏+]
     /*
      * 350. Intersection of Two Arrays II
      * 2016-7-12 by Mingyang
      * 这个就用两个指针来做就好了
      */
     public int[] intersect(int[] nums1, int[] nums2) {
            List<Integer> list = new ArrayList<Integer>();
            Arrays.sort(nums1);
            Arrays.sort(nums2);
            int i = 0;
            int j = 0;
            while (i < nums1.length && j < nums2.length) {
                if (nums1[i] < nums2[j]) {
                    i++;
                } else if (nums1[i] > nums2[j]) {
                    j++;
                } else {
                    list.add(nums1[i]);
                    i++;
                    j++;
                }
            }
            int[] result = new int[list.size()];
            int k = 0;
            for (Integer num : list) {
                result[k++] = num;
            }
            return result;
        }

 

350. Intersection of Two Arrays II

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

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