【一天一道LeetCode】#350. Intersection of Two Arrays II
一天一道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的更多相关文章
- 26. leetcode 350. Intersection of Two Arrays II
350. Intersection of Two Arrays II Given two arrays, write a function to compute their intersection. ...
- [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] 350. Intersection of Two Arrays II 两个数组相交之二
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 (两个数组的相交之二)
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- LeetCode 350. Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- Python [Leetcode 350]Intersection of Two Arrays II
题目描述: Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, ...
- [LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II
这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...
- 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 ...
- 【leetcode】350. Intersection of Two Arrays II
problem 350. Intersection of Two Arrays II 不是特别明白这道题的意思,例子不够说明问题: 是按顺序把相同的元素保存下来,还是排序,但是第二个例子没有重复... ...
随机推荐
- mysql的连接处理过程
在mysqld_main函数中经过一系列的初始化后,mysql开始监听客户端的连接 mysqld_socket_acceptor->connection_event_loop(); 查看my ...
- JSSDK实现微信自定义分享---java 后端获取签名信息
一.首先说下关于微信Access_token的问题,微信Access_token分为2中: 1.授权token获取方式: 这种token需要code值(如何获取code值查看官方文档) "h ...
- Tomcat性能调优-JVM监控与调优
参数设置 在Java虚拟机的参数中,有3种表示方法用"ps -ef |grep "java"命令,可以得到当前Java进程的所有启动参数和配置参数: 标准参数(-),所有 ...
- RabbitMQ用户管理
rabbitmq常用命令 add_user <UserName> <Password> delete_user <UserName> chan ...
- 编程英语之KNN算法
School of Computer Science The University of Adelaide Artificial Intelligence Assignment 2 Semes ...
- south 命令学习
south 命令学习 概述 在django某个版本之前,django自身提供一个创建数据库的命令-syncdb,它会根据model来创建相应的表,但是这个命令不好的地方在于,如果想要对model进行更 ...
- MongoDB 查询分析
MongoDB 查询分析可以确保我们建议的索引是否有效,是查询语句性能分析的重要工具. MongoDB 查询分析常用函数有:explain() 和 hint(). 使用 explain() expla ...
- matplotlib画散点图,并在散点处打上相应标签
运行环境: py3.6 matplotlib 2.1.2 x = [2,4,6,7,8,5,4,3] y = [3,6,5,8,4,3,2,4] txt = ['我','今','晚','上','吃', ...
- dynamic initializer和全局变量
"慎用全局变量,包括全局静态变量" 是众所周知的原则,因为全局变量除了会增加程序的维护成本. 如果全局变量是个复杂的对象,并且还使用其他的全局变量,那情况就变得复杂的多.因为全局变 ...
- 20160210.CCPP体系详解(0020天)
程序片段(01):01.二级指针.c 内容概要:二级指针 #include <stdio.h> #include <stdlib.h> //01.二级指针: // 1.使用场景 ...