LeetCode 349. Intersection of Two Arrays 350. Intersection of Two Arrays II set map使用 ****
来源:互联网 发布:火锅烧烤网络营销策划 编辑:程序博客网 时间:2024/05/21 17:19
之前LeetCode的题主要是对数字,字符串处理,从今天开始做一些查找方面的题。同时需要熟练掌握C++标准库STL为我们提供的容器,算法,主要是map,set,unordered_set,unordered_map的使用。两道题都挺简单,放在一块来写。
一、第一题题目
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1]
, nums2 = [2, 2]
, return[2]
.
Note:
- Each element in the result must be unique.
- The result can be in any order.
注意:返回的结果保证唯一性,返回可以是任意顺序
思路:遍历nums2,当nums2中元素在nums1中,插入到set中,返回结果的唯一性可以用set存储,最后初始化vector
class Solution {public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { set<int> result; for(auto temp:nums2) { auto iter = find(nums1.begin(),nums1.end(),temp); if(iter!=nums1.end()) { result.insert(temp); } } return vector<int>(result.begin(),result.end()); }};上下两者效率一致
class Solution {public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { set<int> result; set<int> record(nums1.begin(),nums1.end()); for(auto temp:nums2) { if(record.find(temp) != record.end()) result.insert(temp); } return vector<int>(result.begin(),result.end()); }};
class Solution {public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { map<int,int> record; for(auto tempnums1:nums1) { record[tempnums1]++; } set<int> res; for (auto tempnums2 : nums2) { if(record[tempnums2]) { res.insert(tempnums2); record[tempnums2]--; } } return vector<int>(res.begin(),res.end()); }};
二、第二题题目
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?
注意:这里求的并集和上述题目还是有微小的差别。
考虑:如果数组已经是有序的改如何优化? 一般有序数组第一个想到的就是二分查找,然后就不需要借助这些数据结构
如果num1的长度小于nums2的长度,那种算法更优
如果num2是存储在硬盘里,并且内存有限不能一次性全部读入,该怎么办?
思路和上述题目类似,代码和最后一种解法类似,唯一的区别就是最后存储的类型不一致
//时间复杂度:O(nlogn)//空间复杂度:O(n)class Solution {public: vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { vector<int> result; map<int,int> record; //unorder_map<int,int> record; 时间复杂度O(n),最终运行效率反而变低了?! for(auto tempnums1:nums1) { record[tempnums1]++; } for(auto temp:nums2) { if(record[temp]) { record[temp]--; result.push_back(temp); } } return result; }};
三.map,set的底层实现区别
map的使用注意事项.
map<int,int> mymap. //map中是有默认值的,对于int来说是0
并且当我们使用过这个键值,那么map会自动的插入这个元素,键为42,值为默认值0
c++语言中set,map底层使用使用的是平衡二叉搜索树,优点就是保持数据的顺序性,顺序性有哪些好处呢
C++语言中unordered_map和unordered_set的底层实现为哈希表,实现快速插入、查找、删除等操作,但是失去了顺序性
- LeetCode 349. Intersection of Two Arrays 350. Intersection of Two Arrays II set map使用 ****
- 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 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】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-350. Intersection of Two Arrays II
- leetcode 350. Intersection of Two Arrays II
- 剑指offer——反转链表
- 程序员面试金典--面试24之二叉树和为某一值的路径
- 笔试面试前突击用
- 经典稳定ssm框架模板搭建——第一节
- C++(4)/new delete & static & this指针 & 全局函数和成员函数
- LeetCode 349. Intersection of Two Arrays 350. Intersection of Two Arrays II set map使用 ****
- 百度面试题之查找最小的K个元素
- HTML相关知识---《基于R语言的自动数据收集》
- height、clientHeight、scrollHeight、offsetHeight区别
- 敌兵布阵 (线段树 单点更新)
- 神经网络的优化算法选择
- 稀疏矩阵的基本操作
- 三层
- Ubuntu vim安装插件YouCompleteMe