350. Intersection of Two Arrays II
题目:
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?
答案:
思路是先排序再比较
1 class Solution { 2 public: 3 vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { 4 sort(nums1.begin(),nums1.end()); 5 sort(nums2.begin(),nums2.end()); 6 int n1=nums1.size(),n2=nums2.size(); 7 vector<int>s; 8 int i=0,j=0; 9 while(i<n1&&j<n2){ 10 if(nums1[i]==nums2[j]){ 11 s.push_back(nums1[i]); 12 i++; 13 j++; 14 } 15 else if(nums1[i]<nums2[j]){ 16 i++; 17 } 18 else{ 19 j++; 20 } 21 } 22 return s; 23 } 24 };
转载于:https://www.cnblogs.com/Reindeer/p/5728923.html
350. Intersection of Two Arrays II相关推荐
- LeetCode 350. Intersection of Two Arrays II
题目: Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, ...
- 350.Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection. 分析: 本题与349题较为相似,唯一不同之处在于交集允许出现重复数字 ...
- 350. Intersection of Two Arrays II 两个数组的交集 II
Title 给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 示例 2: 输入: nums1 = ...
- LeetCode算法题350:两个数组的交集II(Intersection of Two Arrays II)
技术交流可以加: 本人微信:xcg852390212 本人qq:852390212 学习交流qq群1(已满): 962535112 学习交流qq群2: 780902027 两个数组的交集II Leet ...
- C#LeetCode刷题之#350-两个数组的交集 II(Intersection of Two Arrays II)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4044 访问. 给定两个数组,编写一个函数来计算它们的交集. 输入 ...
- [LeetCode] Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1 ...
- 53 两数组的交集(Intersection of Two Arrays)
文章目录 1 题目 2 解决方案 2.1 思路 2.3 时间复杂度 2.4 空间复杂度 3 源码 3.1 排序+合并的方式 3.2 二分搜索的方式 3.3 哈希表的方式 1 题目 题目:两数组的交集( ...
- LeetCode 350. 两个数组的交集 II ntersection of Two Arrays II
Table of Contents 一.中文版 二.英文版 三.My answer 四.解题报告 一.中文版 给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2 ...
- 【leetcode75】Intersection of Two Arrays(数组的交集)
题目描述: 给定两个数组求他们的公共部分,输出形式是数组,相同的元素只是输出一次 例如: nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. 原文描述: ...
- Leetcode: Intersection of Two Arrays
Given two arrays, write a function to compute their intersection.Example: Given nums1 = [1, 2, 2, 1] ...
最新文章
- Jmeter之Beanshell使用(二)Java处理JSON块
- [IOI2008]Island
- mac 打开html c python,在Mac系统使用Visual Studio Code运行Python的方法
- pacemaker+corosync
- api商品分享源码_SSM框架高并发和商品秒杀项目高并发秒杀API源码免费分享
- 敏捷转型谁先动:老总,项目经理or团队
- 组合体视图的画图步骤_(完整版)组合体视图画法教案
- 口语技巧-Part2答题逻辑
- SONiC项目的发展及其介绍
- 〖Python APP 自动化测试实战篇①〗 - 大话闲扯 APP 自动化
- 姑娘美丽人体日志博客_60个美丽诱人的博客设计
- 【Nacos源码篇(五)】Subscriber源码分析
- python 基础一
- cesium中级(二)获取地形高度
- android手机照片恢复,安卓手机照片怎么恢复?简单恢复方法分享
- 自媒体、短视频博主都在用这5个免费视频网站
- 计算机进制、内存模块详讲-入门必看(超详细)
- USB驱动始终不能成功下载安装
- 编译sqlite3时报错【‘GetVersionExA‘: 被声明为已否决】的解决办法
- 大整数加法【信息学奥赛一本通-1168】
热门文章
- mac下nginx安装及与tomcat简单配置
- 数字字符减去数字字符0就是数字
- 站在过去的肩膀上(一分钟能做什么?:不可思议的“战拖”心理学)
- SQL的3种连接查询
- linux离线安装docker + docker-compose
- mybatis+oracle实现简单的模糊查询
- com.microsoft.sqlserver.jdbc.SQLServerException: Socket closed 或者 该连接已关闭
- 181101新闻:午后阳光下集思广益,课例研修尝试与挑战并存
- jdk基础之Object类getClass()方法
- vue-music 关于搜索历史本地存储