一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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?

(二)解题

题目大意:找两个数组的交集,数组中有重复的数。

题目思路:【一天一道LeetCode】#349. Intersection of Two Arrays与上题差不多,只不过这道题不需要考虑去重的问题,所以,去掉重复检测那段就行。

class Solution {
public:
    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
        vector<int> ret;
        if(nums1.empty()||nums2.empty()) return ret;
        sort(nums1.begin(),nums1.end());//首先对两个数组排序
        sort(nums2.begin(),nums2.end());
        auto iter1 = nums1.begin();
        auto iter2 = nums2.begin();
        auto end1 = nums1.end();
        auto end2 = nums2.end();
        while(iter1 != end1&&iter2!=end2)//其中一个遍历完就退出
        {
            if(*iter1<*iter2){//*iter2大就把iter1往后找
                ++iter1;
            }
            else if(*iter1>*iter2){//*iter1大就把iter2往后找
                ++iter2;
            }
            else {//找到了交集
                ret.push_back(*iter1);//存储交集
                ++iter1;
                ++iter2;
            }
        }
        return ret;
    }
};

【一天一道LeetCode】#350. Intersection of Two Arrays II的更多相关文章

  1. 26. leetcode 350. Intersection of Two Arrays II

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

  2. [LeetCode] 350. Intersection of Two Arrays II 两个数组相交II

    Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...

  3. [LeetCode] 350. Intersection of Two Arrays II 两个数组相交之二

    Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...

  4. LeetCode 350. Intersection of Two Arrays II (两个数组的相交之二)

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  5. LeetCode 350. Intersection of Two Arrays II

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  6. Python [Leetcode 350]Intersection of Two Arrays II

    题目描述: Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, ...

  7. [LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II

    这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...

  8. LeetCode Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II

    169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = funct ...

  9. 【leetcode】350. Intersection of Two Arrays II

    problem 350. Intersection of Two Arrays II 不是特别明白这道题的意思,例子不够说明问题: 是按顺序把相同的元素保存下来,还是排序,但是第二个例子没有重复... ...

随机推荐

  1. SpringBoot学习之mvc

    Spring Boot非常适合Web应用程序开发. 我们可以使用嵌入式Tomcat,Jetty或Undertow轻松创建自包含的HTTP服务器. 大多数Web应用程序将使用spring-boot-st ...

  2. 实现鼠标双击(OnGUI)

    void OnGUI()//实现鼠标双击 { Event Mouse = Event.current; if (Mouse.isMouse && Mouse.type == Event ...

  3. 如何在Google上下载高清原图

    在我们学习和生活中常常一些高清图片作为相关的素材,比如制作PPT.写博文.制作视频都需要大量图片.我们常常会在百度上下载一些图片,但是百度上提供的图片存在很多问题:存在水印.清晰度不够等.而Googl ...

  4. StarSpace是用于高效学习实体向量的通用神经模型

    StarSpace是用于高效学习实体向量的通用神经模型,用于解决各种各样的问题: 学习单词,句子或文档级嵌入. 文本分类或任何其他标签任务. 信息检索:实体/文件或对象集合的排序,例如 排名网络文件. ...

  5. JSON.parse()在火狐中的BUG

      //用sessionStorage解决load页面刷新问题 { //sessionStorage.removeItem("loadInfo"); var loadInfo=de ...

  6. 软件测试assert

    之前实习做过一段时间测试,现做个总结: 实习测试的是一款CM系统(case 系统),来记录IT部门处理的维修,服务,反馈,预定服务等case:b/s架构,人少小项目,实习时间短,去了已经快完工,主要测 ...

  7. Promise--优雅的异步回调解决方案

    当一个接口需要依赖另一个接口的请求数据时,通常有两种解决方式,一个是将请求数据的接口设为同步,之后调另一个接口,另一个是在请求数据接口的成功回调里调另一个接口. 但是:当一个接口需要依赖很多个接口的请 ...

  8. 用命令直接在两台ubuntu之间传输数据

    首先查看openssh-server是否启动: ps -e | grep ssh 如果没有任何提示则是没有启动: sudo /etc/init.d/ssh -start 启动进程.若提示找不到命令则需 ...

  9. git提交项目常用命令及git分支的用法

    1.第一步首先从git托管平台clone项目,我这里就使用idea为例: 填写git的url与存放本地目录名及项目名     2.如果你对项目进行了一些修改,就可以执行git命令,进行提交. 有两种方 ...

  10. oh forever love~

    npm install -g forever forever start c9sdk/server.js --listen 0.0.0.0 --port 80 -a aa:111 -w ~ To el ...