关于LeetCode中Intersection of Two Arrays II一题的理解

题目如下:

Given two arrays, write a function to compute their intersection.

Example:
Given nums1 = [1, 2, 2, 1]nums2 = [2, 2], return [2, 2].

Note:

  • Each element in the result should appear as many times as it shows in both arrays.
  • The result can be in any order.

Follow up:

  • What if the given array is already sorted? How would you optimize your algorithm?
  • What if nums1's size is small compared to nums2's size? Which algorithm is better?
  • What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?

    题目的意思是给定两个Int类型的数组nums1和nums2,返回一个int类型的新数组,这个数组中应该包含nums1和nums2中共同的部分。什么叫做共同的部分呢?比如说nums1=[1,2,2,1],nums2=[2,2],那结果就应该返回[2,2]。因为[2,2]这两个数字是nums1中和nums2中都出现的部分。之前有一道题“Intersection of Two Arrays”中如果还是刚才的例子,返回的结果就应该是[2]。因为那道题最后返回的是两个数组中出现的“共同数字”,因为数字“2”在nums1和nums2中都出现过,跟它共同出现在这两个数组中的次数是没有关系的,但是这道题就有关系了,所以要弄清楚题干表达的意思。

    遇到这种问题的思路很显然是用HashMap(啊呸!),这次不用HashMap了(虽然它当然能够完成这道题目)。我们看下面的Follow Up中提示假如给定的Array是已经拍好顺序的,算法是不是会简单一些呢?好,我们就顺着这个思路,先将nums1和nums2进行排序,我们需要的是什么样的元素呢?假设nums1中的第i位和nums2的第j位相等,我们就应该把nums1[i](或nums2[j])放入最终要输出的结果数组中去。然后查看nums1中的第i+1位和nums2中的第j+1位是否相等。如果不相等肯定就分为两种情况,(1)nums1[i+1]>nums2[j+1],由于nums1和nums2已经是拍好序的了,nums1[i+1]>nums2[j+1]说明在nums2中与nums1[i+1]相等的元素必定在j+1的后面,所以我们要保持i+1的位置不变将j继续向后移动一位到j+2的位置,然后继续比较nums1[i+1]和nums2[j+2]的大小;(2)nums1[i+1]<nums2[j+1],和上面的情况同理,我们要保持j+1的位置不变将i继续向后移动一位到i+2的位置,然后继续比较nums1[i+2]和nums2[j+1]的大小;什么时候应该停止移动呢?就是i的值等于nums1数组的大小,或者j的长度等于nums2数组大小的时候。这时候i和j其中之一刚刚遍历完nums1或nums2,继续遍历剩下的较长数组已经毫无意义了,此时就应该结束循环,并输出在遍历过程中逐步得到的结果。已经Accepted的代码如下:

<span style="font-size:14px;">    public int[] intersect(int[] nums1, int[] nums2) {
        int nums1_index = 0;
        int nums2_index = 0;
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        ArrayList<Integer> result = new ArrayList<Integer>();
        while (nums1_index < nums1.length && nums2_index < nums2.length) {
            if (nums1[nums1_index] == nums2[nums2_index]){
                result.add(nums1[nums1_index]);
                nums1_index++;
                nums2_index++;
            }else if (nums1[nums1_index] < nums2[nums2_index]){
                nums1_index++;
            }else{
                nums2_index++;
            }
        }
        int[] finalResult = new int[result.size()];
        for (int j=0;j<finalResult.length;j++){
            finalResult[j] = result.get(j);
        }
        return finalResult;
    }</span>

    然后是评论区的代码,使用HashMap的代码如下,这个真没有什么好说的,之前用的太多了。

<span style="font-size:14px;">    public int[] intersect(int[] nums1, int[] nums2) {
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        ArrayList<Integer> result = new ArrayList<Integer>();
        for(int i = 0; i < nums1.length; i++)
        {
            if(map.containsKey(nums1[i])) map.put(nums1[i], map.get(nums1[i])+1);
            else map.put(nums1[i], 1);
        }
    
        for(int i = 0; i < nums2.length; i++)
        {
            if(map.containsKey(nums2[i]) && map.get(nums2[i]) > 0)
            {
                result.add(nums2[i]);
                map.put(nums2[i], map.get(nums2[i])-1);
            }
        }
    
       int[] r = new int[result.size()];
       for(int i = 0; i < result.size(); i++)
       {
           r[i] = result.get(i);
       }
    
       return r;
    }</span>

    终于有一次一次就直接Accepted的代码了,感觉真是excited!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值