【一天一道LeetCode】#349. Intersection of Two Arrays
一天一道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的更多相关文章
- [LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II
这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...
- [LeetCode] 349. Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...
- LeetCode 349. Intersection of Two Arrays (两个数组的相交)
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- LeetCode 349. Intersection of Two Arrays
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- 15. leetcode 349. Intersection of Two Arrays
Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1 ...
- LeetCode 349 Intersection of Two Arrays 解题报告
题目要求 Given two arrays, write a function to compute their intersection. 题目分析及思路 给定两个数组,要求得到它们之中共同拥有的元 ...
- [leetcode]349. Intersection of Two Arrays数组交集
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...
- [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 ...
- 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 ...
- Python 解LeetCode:Intersection of Two Arrays
最近,在用解决LeetCode问题的时候,做了349: Intersection of Two Arrays这个问题,就是求两个列表的交集.我这种弱鸡,第一种想法是把问题解决,而不是分析复杂度,于是写 ...
随机推荐
- Python【第四课】 装饰器
本篇内容 什么是装饰器 装饰器需要遵循的原则 实现装饰器的知识储备 高阶函数 函数嵌套 闭包函数 无参函数 装饰器示例 1.什么是装饰器 器即函数 装饰即修饰,意指为其他函数添加新功能 装饰器定义:本 ...
- .net 导入excel数据
using System; using System.Data; using System.Data.OleDb; using System.Data.SqlClient; using System. ...
- js去除空格,判断是否包含
js去除空格 function trimStr(str){ return str.replace(/(^\s*)|(\s*$)/g,""); } js判断是否包含 //是否包含 f ...
- LeetCode 2
No1 Given a sorted array and a target value, return the index if the target is found. If not, return ...
- 06 Nexus仓储/基础设施 - DevOps之路
06 Nexus仓储/基础设施 - DevOps之路 文章Github地址,欢迎start:https://github.com/li-keli/DevOps-WiKi Nexus仓储官网简介: Th ...
- WSGI及gunicorn指北(一)
作为一个Python Web 开发工程师,pyg0每天都喜滋滋的写着基于各种web框架的业务代码. 突然有一天,技术老大过来跟pyg0说,嘿,我们要新上线一个服务,你来帮我部署一下吧.不用太复杂.用g ...
- Node.js 流
稳定性: 2 - 不稳定 流是一个抽象接口,在 Node 里被不同的对象实现.例如request to an HTTPserver 是流,stdout 是流.流是可读,可写,或者可读写.所有的流是 E ...
- Django 缓存模块 page_cache 源码阅读
Django cache中比较常用的有 cache_page 这么个 decorators, 下面就根据请求流程,结合源码来说说它是怎么工作的? 版本是django1.8,不同版本可能函数等会变化,逻 ...
- uboot-tiny4412启动流程(下)----如何将自己的裸板测试程序加入uboot中启动测试
今天在工作上搞了一天高通的芯片uboot程序,目的是希望将一个裸板的程序移植到uboot中,并且开机让它运行.这个芯片是NXP4330,目前是高通的一个芯片,基于ARM-contexA9架构,那么就跟 ...
- ROS机器人程序设计(原书第2版)补充资料 (捌) 第八章 导航功能包集入门 navigation
ROS机器人程序设计(原书第2版)补充资料 (捌) 第八章 导航功能包集入门 navigation 书中,大部分出现hydro的地方,直接替换为indigo或jade或kinetic,即可在对应版本中 ...