leetcode——Intersection of Two Arrays II
来源:互联网 发布:逆波兰式计算c语言 编辑:程序博客网 时间:2024/06/10 06:11
题目:
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1]
, nums2 = [2, 2]
, return [2, 2]
.
class Solution {public: vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { vector<int> res; unordered_map<int, int> m; for (auto i : nums1) { m[i]++; } for (auto i : nums2) { //if(m[i]-- > 0) res.push_back(i);//如果用这个空间复杂度就是m+n了 auto it = m.find(i); if (it != m.end() && it->second-- > 0)//空间复杂度是n { res.push_back(i); } } return res; }};
class Solution {public: vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { //sort(nums1.begin(), nums1.end()); //sort(nums2.begin(), nums2.end()); int n1 = (int)nums1.size(), n2 = (int)nums2.size(); int i1 = 0, i2 = 0; vector<int> res; while(i1 < n1 && i2 < n2){ if(nums1[i1] == nums2[i2]) { res.push_back(nums1[i1]); i1++; i2++; } else if(nums1[i1] > nums2[i2]){ i2++; } else{ i1++; } } return res; }};
0 0
- leetcode——Intersection of Two Arrays II
- Leetcode——350. Intersection of Two Arrays II
- Hold住Leetcode——Intersection of Two Arrays II
- leetcode——Intersection of Two Arrays
- [LeetCode] Intersection of Two Arrays II
- LeetCode Intersection of Two Arrays II
- LeetCode 350. Intersection of Two Arrays II
- LeetCode:Intersection of Two Arrays II
- leetcode.350. Intersection of Two Arrays II
- LeetCode 350:Intersection of Two Arrays II
- [leetcode] 350. Intersection of Two Arrays II
- Leetcode 350. Intersection of Two Arrays II
- leetcode 350. Intersection of Two Arrays II
- leetcode Intersection of Two Arrays II
- Leetcode 350. Intersection of Two Arrays II
- [python][leetcode]Intersection of Two Arrays II
- [leetcode] 350. Intersection of Two Arrays II
- leetcode 350. Intersection of Two Arrays II
- spring MVC框架下 前台向java后台传送json字符串数据
- UVA10719Quotient Polynomial
- Android中的ScrollView
- 大素数测试和大数素因子分解
- The MySQL server is running with the --read-only option so it cannot execute this statement
- leetcode——Intersection of Two Arrays II
- 某服务请求经负载均衡设备分配到集群A、B、C、D进行处理响应的概率分别是10%、20%、30%和40%。已知测试集群所得的稳定性指标分别是90%、95%、99%和99.9%。现在该服务器请求处理失败,
- js实现全选和反选功能
- 小心!新媒体环境下,营销传播还有几个大坑!
- unity中鼠标和触摸问题
- 什么是野指针?如何避免野指针?
- HDU 5562 Clarke and five-pointed star (BC)
- July 15th 模拟赛C T4 回家(莫名其妙【推荐】) Solution
- [Database] 大数据量高并发的数据库优化详解