day24【LeetCode力扣】350.两个数组的交集2️⃣
1.题目描述
给你两个整数数组 nums1
和 nums2
,请你以数组形式返回两数组的交集。返回结果中每个元素出现的次数,应与元素在两个数组中都出现的次数一致(如果出现次数不一致,则考虑取较小值)。可以不考虑输出结果的顺序。
示例 1:
输入:nums1 = [1,2,2,1], nums2 = [2,2]
输出:[2,2]
示例 2:
输入:nums1 = [4,9,5], nums2 = [9,4,9,8,4]
输出:[4,9]
提示:
1 <= nums1.length, nums2.length <= 1000
0 <= nums1[i], nums2[i] <= 1000
2.题解
这个题目和上一个题有什么区别呢?其实本质上是一样的,但是它的返回结果中出现了重复的元素,那么此时就不能用集合来表示了。
此时我们注意到题目给出了数值范围,那么我们可以用数组表示哈希表。
c++
1️⃣使用数组哈希
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
vector<int> result;
int hash[1001]={0};
for(int num : nums1){
hash[num]++;
}
for(int num : nums2){
if(hash[num] >= 1){
result.push_back(num);
hash[num]--;
}
}
return result;
}
};
2️⃣使用unordered_map映射的方法
也可以直接做
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
vector<int> result;
unordered_map<int, int> hash;
for (int num : nums1) {
hash[num]++;
}
for (int num : nums2) {
if (hash.find(num) != hash.end() && hash[num] > 0) {
result.push_back(num);
hash[num]--;
}
}
return result;
}
};
这儿的hash.find(num) != hash.end()
这行代码可以有很多种表现形式,hash.count(num)
3️⃣双指针+排序
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
sort(nums1.begin(),nums1.end());
sort(nums2.begin(),nums2.end());
int length1 = nums1.size(),length2 = nums2.size();
vector<int> res;
int index1 = 0,index2 = 0;
while (index1<length1 && index2< length2){
if(nums1[index1]<nums2[index2])
index1++;
else if(nums1[index1] == nums2[index2]){
res.push_back(nums1[index1]);
index1++;
index2++;
}
else{
index2++;
}
}
return res;
}
};
python
1️⃣数组解法
class Solution:
def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
result = []
hash_num =[0]*1001
for i in nums1:
hash_num[i] += 1
for i in nums2:
if hash_num[i]>=1:
result.append(i)
hash_num[i] -= 1
return result
class Solution:
def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
result = []
hash_num =[0]*1001
for i in nums1:
if i in nums2:
result.append(i)
nums2.remove(i)
return result
2️⃣使用counter计数
class Solution:
def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
res = []
a_count = Counter(nums1)
for num in nums2:
if a_count.get(num,0) > 0:
res.append(num)
a_count[num] -= 1
return res
3️⃣使用defaultdict
class Solution:
def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
res = []
a_count = defaultdict(int)
for num in nums1:
a_count[num] += 1
for j in nums2:
if a_count[j] > 0:
res.append(j)
a_count[j] -= 1
return res
4️⃣排序+双指针
class Solution:
def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
nums1.sort()
nums2.sort()
m,n = len(nums1),len(nums2)
index1,index2 = 0,0
res = []
while index1<m and index2 <n:
if nums1[index1] < nums2[index2]:
index1 += 1
elif nums1[index1] == nums2[index2]:
res.append(nums1[index1])
index1 += 1
index2 += 1
else:
index2 += 1
return res
ok解法很多,大家自己去探索吧~~~~
如果觉得作者写的不错,求给博主一个大大的点赞支持一下,你们的支持是我更新的最大动力!
如果觉得作者写的不错,求给博主一个大大的点赞支持一下,你们的支持是我更新的最大动力!
如果觉得作者写的不错,求给博主一个大大的点赞支持一下,你们的支持是我更新的最大动力!