【一天一道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 不是特别明白这道题的意思,例子不够说明问题: 是按顺序把相同的元素保存下来,还是排序,但是第二个例子没有重复... ...
随机推荐
- SpringCloud学习之eureka集群配置
一.集群方案及部署思路: 如果是单节点的注册中心,是无法保证系统稳定性的,当然现在项目部署架构不可能是单节点的. 集群节点的部署思路:通过运行多个实例并请求他们相互注册,来完成注册中心的高可用性(结伴 ...
- Python 线程池,进程池,协程,和其他
本节内容 线程池 进程池 协程 try异常处理 IO多路复用 线程的继承调用 1.线程池 线程池帮助你来管理线程,不再需要每个任务都创建一个线程进行处理任务. 任务需要执行时,会从线程池申请线程,有则 ...
- C语言程序设计预报作业
1. 阅读邹欣老师的博客--师生关系,针对文中的几种师生关系谈谈你的看法,你期望的师生关系是什么样的? 答:万物都是变化的,师生关系也一样.小学中学把老师看作春蚕,蜡烛的的比喻到了大学显然行不通了.大 ...
- JAX-RPC 与 JAX-WS 的比较
引言 Web 服务已经出现很久了.首先是 SOAP,但 SOAP 仅描述消息的情况,然后是 WSDL,WSDL 并不会告诉您如何使用 Java™ 编写 Web 服务.在这种情况下,JAX-RPC 1. ...
- node上传文件并在网页中展示
一.需求 1.当用户请求http://domain/start时,可以看到一个欢迎页面,页面上有一个文件上传的表单. 2.用户可以选择一个图片并提交表单,随后文件将被上传到http://domain/ ...
- JAVA 练习 找出素数
package com.zhang.hello; public class Task { /** * 1. 输出打印九九乘法表 * */ public void NO1(){ for(int i=1; ...
- seaborn使用(绘图函数)
seaborn使用(绘图函数) 数据集分布的可视化 分类数据的绘图 线性关系可视化 一.数据集分布的可视化 distplot kdeplot rugplot 1.distplot() 灵活的绘制单变量 ...
- JVM初探- 内存分配、GC原理与垃圾收集器
JVM初探- 内存分配.GC原理与垃圾收集器 标签 : JVM JVM内存的分配与回收大致可分为如下4个步骤: 何时分配 -> 怎样分配 -> 何时回收 -> 怎样回收. 除了在概念 ...
- Android Studio精彩案例(七)《ToolBar使用详解<一>》
转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 本文参考博客:http://blog.csdn.net/h_zhang/article/details/51232773 http:/ ...
- JVM:类的生命周期
类的生命周期 综述 1. 只有当一个类被切实使用到的时候才会被加载到虚拟机中(例如:new, 方法调用, A a = null;不算) 2. 若在加载一个类的过程中,有其他类被切实使用到, ...