本题来自链接: https://leetcode-cn.com/problems/intersection-of-two-arrays-ii/.题解参考官方解答。
1.哈希表
unordered_map是c++ 哈希表的实现模板
哈希表的性质:
-
一个key值对应一个value,不允许key有重复;
-
最适合的求解问题是查找与定值相等的记录;
-
可在常数时间通过key值,找到value
1.1判断某个键值是否存在
- find函数,
iterator find(const key_type& key);
如果Key值存在,则返回其迭代器;如果key值不存在,则返回 unordered_map::end;
m.find()==m.end();不存在,则返回true
2.count函数
size_type count (const kry_type& key) const;
count函数统计key值出现的次数,因为哈希表不允许重复,因此当key存在,则返回1,不存在则返回0;
1.2迭代器遍历
for(int num : nums)
相当于
for(iterator it = begin();it < end(); it++)
对容器里的每个元素进行遍历,num相当于每个元素的别名
1.3代码实现
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
//为了降低空间复杂度,首先遍历较短的数组并在哈希表中记录
//每个数字以及对应出现的次数,然后遍历较长的数组得到交集。
if(nums1.size()>nums2.size())
{
return intersect(nums2,nums1);
}
unordered_map<int,int> m;
for(int num : nums1)
{
++m[num];
}
vector <int> intercection;
for(int num : nums2){
if(m.count(num))
{
intercection.push_back(num);
m[num]--;
if(m[num]==0){
m.erase(num);
}
}
}
return intercection;
}
};
复杂度分析:
时间复杂度:O(m+n)O(m+n),其中 mm 和 nn 分别是两个数组的长度。需要遍历两个数组并对哈希表进行操作,哈希表操作的时间复杂度是 O(1)O(1),因此总时间复杂度与两个数组的长度和呈线性关系。
空间复杂度:O(\min(m,n))O(min(m,n)),其中 mm 和 nn 分别是两个数组的长度。对较短的数组进行哈希表的操作,哈希表的大小不会超过较短的数组的长度。为返回值创建一个数组 intersection,其长度为较短的数组的长度。
2.双指针
1.对两个数组进行排序
2.初始时,两个指针指向两个数组的头部;
3.每次比较两个指针指向数组的大小,如果不相等,则数字较小的的指针+1;如果相等,则两个指针同时+1;
4.直到任意一个指针超出数组范围,遍历结束;
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
int length1 = nums1.size();
int length2 = nums2.size();
sort(nums1.begin() , nums2.end());
sort(nums2.begin() , nums2.end());
int index1=0,index2=0;
vector <int> intersection;
while(index1<length1&&index2<length2)
{
if(nums1[index1]<nums2[index2])
{
index1++;
}
else if(nums1[index1]>nums2[index2])
{
index2++;
}
else
{
intersection.push_back(nums1[index1]);
index1++;
index2++;
}
}
return intersection;
}
};
程序出现问题,但不知道为啥,报错:
45ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000000084 at pc 0x0000003927a9 bp 0x7fff746999b0 sp 0x7fff746999a8
READ of size 4 at 0x602000000084 thread T0
#8 0x7f1a4234082f (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
0x602000000084 is located 12 bytes to the left of 1-byte region [0x602000000090,0x602000000091)
freed by thread T0 here:
#6 0x7f1a4234082f (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
previously allocated by thread T0 here:
#14 0x7f1a4234082f (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
Shadow bytes around the buggy address:
0x0c047fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff8000: fa fa fd fa fa fa fd fa fa fa 00 00 fa fa fd fa
=>0x0c047fff8010:[fa]fa fd fa fa fa 00 fa fa fa fa fa fa fa fa fa
0x0c047fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
Shadow gap: cc
45ABORTING