Intersection of Two Arrays&Intersection of Two Arrays II

  1. Intersection of Two Arrays
    Given two arrays, write a function to compute their intersection.

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

Note:
Each element in the result must be unique.
The result can be in any order.
JS代码:

var intersection = function(nums1, nums2) {
    var intersect=[];
    // var k=0;
    // var j=0,i=0;
    // if(nums1.length===0||nums2.length===0)
    // {
    //     return intersect;
    // }
    // nums1.sort(function(a,b){  //数组升序排列
    //     return a-b;
    // });
    // nums2.sort(function(a,b){  //数组升序排列
    //     return a-b;
    // });


    //     while((j<nums1.length)&&(i<nums2.length))  
    //     {
    //         while(nums2[i]<=nums1[j])  
    //       {

    //         if(nums2[i]===nums1[j])
    //         {       
    //             if(k===0)
    //             {
    //                 intersect[0]=nums2[i];
    //                     k++; 
    //                     i++;
    //                     j++;
    //           }
    //           else{
    //                   if(nums2[i]!==intersect[k-1])
    //                 {
    //                     intersect[k]=nums2[i];
    //                     k++;

    //                 }
    //                  i++;
    //               j++;
    //           }

    //         }else {
    //           i++;     //若nums2[i]<nums1[j],则增大nums2[i]
    //         }

    //     }
    //      j++;   //若nums2[i]>nums1[j],则增大nums2[j]
    //     }
    if(nums1===null||nums2===null)
    {
        return null;
    }
    var num1Obj={};
    var num2Obj={};
    var len1=nums1.length;
    var len2=nums2.length;
    for(var i=0;i<len1;i++)//对象num1Obj存储数组num1中的值
    {
        if(!num1Obj.hasOwnProperty(nums1[i]))
        {
            num1Obj[nums1[i]]=1;
        }else{
            num1Obj[nums1[i]]++;
        }
    }
    for(var j=0;j<len2;j++)//对象num2Obj存储数组num2中的值
    {
        if(!num2Obj.hasOwnProperty(nums2[j]))
        {
            num2Obj[nums2[j]]=1;
        }else{
            num2Obj[nums2[j]]++;
        }
    }
   for(var k in num1Obj)
   {
       if(num2Obj.hasOwnProperty(k))
       {
           intersect.push(parseInt(k));
       }
   }
    return intersect;
};

2.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?

public class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        Map<Integer, Integer> map1 = new HashMap<Integer, Integer>();
        for (int num : nums1) {
            if (map1.containsKey(num)) {
                map1.put(num, map1.get(num) + 1);
            } else {
                map1.put(num, 1);
            }
        }

        Map<Integer, Integer> map2 = new HashMap<Integer, Integer>();
        for (int num : nums2) {
            if (map2.containsKey(num)) {
                map2.put(num, map2.get(num) + 1);
            } else {
                map2.put(num, 1);
            }
        }

        List<Integer> list = new ArrayList<Integer>();
        for (Map.Entry<Integer, Integer> entry : map1.entrySet()) {
            int times = entry.getValue();
            if (map2.containsKey(entry.getKey())) {
                times = Math.min(times, map2.get(entry.getKey()));
            } else {
                times = 0;
            }
            for (int i = 0; i < times; i++) {
                list.add(entry.getKey());
            }
        }

        int[] res = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            res[i] = list.get(i);
        }
        return res;
    }
}

总结:做这两个算法题最大的收获就是学会了一种时间复杂度为O(n)排序的算法——计数排序算法。
计数排序算法:
以下是我在别人博客上看到的相关解释,通俗易懂,所以粘贴于此。
不过实际上,在数字范围有限制的情况下,(必须是数字才行的吧)是有一个这样的算法的,只需要用一个数组记录每个数字出现次数就可以了。
假定你的数字范围在0到65535范围之内,定义一个数组count[65536](这个空间是常量,和n无关,所以是O(1) ),初值全部为0。
那么假设有下面这些数字:
100
200
300
119
0
6

那么对于每个这个数字,都做在count中记录一下:
100 => count[100]++
200 => count[200]++
300 => count[300]++
119 => count[119]++
0 => count[0]++
6 => count[6]++

最后,遍历一边所有这些数字就可得到0~65535每个数字的个数(在count数组中),然后再顺序遍历count数组,count[n] = m,则输出m个n,(比如说有count[3] = 2, 那么说明有2个数字3),依次输出,最后可得结果。第一次遍历是O(n),第二次遍历是O(1),为常量,所以最后的时间复杂度为O(n),而空间复杂度为O(1)
这个算法很简单,相信大家都会,只是这个题太过于变态了,一般会把面试者吓住

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值