Intersection of Two Arrays II
来源:互联网 发布:网络盗刷信用卡能查到 编辑:程序博客网 时间:2024/05/18 09:03
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?
Subscribe to see which companies asked this question
代码
class Solution { public: vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { sort(nums1.begin(),nums1.end()); sort(nums2.begin(),nums2.end()); int len1=nums1.size(); int len2=nums2.size(); vector<int> result; int t1=0; int t2=0; while(t1<len1 && t2<len2) { if(nums1[t1]==nums2[t2] ) { result.push_back(nums1[t1]); t1++; t2++; } else if(nums1[t1]<nums2[t2]) t1++; else t2++; } return result; } };
0 0
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Intersection of Two Arrays&Intersection of Two Arrays II
- [LeetCode] Intersection of Two Arrays II
- 350. Intersection of Two Arrays II
- LeetCode Intersection of Two Arrays II
- CCU(网络游戏用语)的解释
- Spark实战----(1)使用Scala开发本地测试的Spark WordCount程序
- CSS实现三栏式布局
- MyBatis处理表与表之间的关系
- 前端性能优化:创建对象二
- Intersection of Two Arrays II
- nginx之ngx_http_limit_req
- 专题四 Problem A
- [Java并发包学习一]Executor和ExecutorService
- MFC状态栏的编程(SDI)
- css input select option 重置样式
- IntelliJ IDEA 快捷键
- 创意图形项目技术文章总结
- WebGL 技术储备指南