class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
vector<int> res;
unordered_map<int, int> m;
for(const auto &x : nums1){ //const auto&声明迭代变量,可以确保在循环中不会修改容器中的元素,同时避免不必要的对象拷贝,提高效率
m[x] = 1;
}
for(const auto &x: nums2){
if(--m[x] == 0){
res.push_back(x);
}
}
return res;
}
};
LeetCode 349. 两个数组的交集
于 2024-08-12 23:18:21 首次发布