一天一道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].

Note:

  • Each element in the result must be unique.

  • The result can be in any order.

(二)解题

class Solution {
public:
    vector<int> intersection(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;
                while(iter1<end1&&*iter1==*(iter1-1)) ++iter1;//去除重复
                while(iter2<end2&&*iter2==*(iter2-1)) ++iter2;//同上

            }
        }
        return ret;
    }
};

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

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

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

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

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

  3. LeetCode 349. Intersection of Two Arrays (两个数组的相交)

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

  4. LeetCode 349. Intersection of Two Arrays

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

  5. 15. leetcode 349. Intersection of Two Arrays

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

  6. LeetCode 349 Intersection of Two Arrays 解题报告

    题目要求 Given two arrays, write a function to compute their intersection. 题目分析及思路 给定两个数组,要求得到它们之中共同拥有的元 ...

  7. [leetcode]349. Intersection of Two Arrays数组交集

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

  8. [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 ...

  9. LeetCode Javascript实现 283. Move Zeroes 349. Intersection of Two Arrays 237. Delete Node in a Linked List

    283. Move Zeroes var moveZeroes = function(nums) { var num1=0,num2=1; while(num1!=num2){ nums.forEac ...

  10. Python 解LeetCode:Intersection of Two Arrays

    最近,在用解决LeetCode问题的时候,做了349: Intersection of Two Arrays这个问题,就是求两个列表的交集.我这种弱鸡,第一种想法是把问题解决,而不是分析复杂度,于是写 ...

随机推荐

  1. 四种常用IO模型

    1) 同步阻塞IO(Blocking IO)2) 同步非阻塞IO(Non-blocking IO)3) IO多路复用(IO Multiplexing)4) 异步IO(Asynchronous IO) ...

  2. 整理spring定时器corn表达式

    1.结构 corn从左到右(用空格隔开):秒 分 小时 月份中的日期 月份 星期中的日期 年份 2.各字段的含义   字段 允许值 允许的特殊字符 秒 0~59 - * / 分 0~59 - * / ...

  3. Kibana插件sentinl使用教程

    简介 对于Kibana的一些数据我们有时候是想要对某些字段进行持续关注的,这时候通过报警的手段就可以大幅提升对这些信息状态了解的及时性及可靠性.使用sentinl插件就可以帮助我们实现这个功能. 此教 ...

  4. angular 路由的引用

    使用angular路由 遇到的坑. 使用cmd 安装好node.js 安装成功 输入node  -v 能查看版本说明安装成功 搭建angular项目输入命令 npm install  -g  angu ...

  5. map函数、filer函数、reduce函数的用法和区别

    Map函数 map函数的用法如下: def add_one(x): return x+1 #使用普通函数 v1 = map(add_one,[1,2,3]) v1 = list(v1) print(v ...

  6. jQuery 学习笔记一

  7. jQuery 效果 – 动画

    在使用jQuery动画时,你可能想要实现更加丰富的效果,那么你可以通过使用 jQuery animate() 方法自定义动画来达到目的,具体的使用方法如下文所述. jQuery animate() 方 ...

  8. SOAP Binding: Difference between Document and RPC Style Web Services

    SOAP Binding: Difference between Document and RPC Style Web Services 20FLARES Twitter 1Facebook 9Goo ...

  9. Android Studio 如何打JAR包

    Android Studio 如何打JAR包 在eclipse中我们知道如何将一个项目导出为jar包,供其它项目使用.  在AS中可以通过修改gradle才处理.  我们新建一个项目MakeJar,在 ...

  10. Android开发学习之路--基于vitamio的视频播放器(一)

      之前也试过vitamio这个库,后来不知道被什么事情给耽搁了,就没继续下去.近来觉得视频还是需要学习一下的,谁让直播那么火呢,就想着写一个简单的视频播放的app先吧.好了那就开始吧,暂时取名为JP ...