[LintCode] Intersection of Two Arrays 两个数组相交
数组 of 两个 two lintcode Arrays 相交 intersection
2023-09-11 14:21:37 时间
Given two arrays, write a function to compute their intersection.
Notice
Each element in the result must be unique.
The result can be in any order.
Have you met this question in a real interview?
Example
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].
Challenge
Can you implement it in three different algorithms?
LeetCode上的原题,请参见我之前的博客Intersection of Two Arrays。
解法一:
class Solution { public: /** * @param nums1 an integer array * @param nums2 an integer array * @return an integer array */ vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { set<int> s, res; for (auto a : nums1) s.insert(a); for (auto a : nums2) { if (s.count(a)) res.insert(a); } return vector<int>(res.begin(), res.end()); } };
解法二:
class Solution { public: /** * @param nums1 an integer array * @param nums2 an integer array * @return an integer array */ vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { vector<int> res; int i = 0, j = 0; sort(nums1.begin(), nums1.end()); sort(nums2.begin(), nums2.end()); while (i < nums1.size() && j < nums2.size()) { if (nums1[i] < nums2[j]) ++i; else if (nums1[i] > nums2[j]) ++j; else { if (res.empty() || res.back() != nums1[i]) { res.push_back(nums1[i]); } ++i; ++j; } } return res; } };
解法三:
class Solution { public: /** * @param nums1 an integer array * @param nums2 an integer array * @return an integer array */ vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { set<int> res; sort(nums2.begin(), nums2.end()); for (auto a : nums1) { if (binarySearch(nums2, a)) { res.insert(a); } } return vector<int> (res.begin(), res.end()); } bool binarySearch(vector<int> &nums, int target) { int left = 0, right = nums.size(); while (left < right) { int mid = left + (right - left) / 2; if (nums[mid] == target) return true; else if (nums[mid] < target) left = mid + 1; else right = mid; } return false; } };
解法四:
class Solution { public: /** * @param nums1 an integer array * @param nums2 an integer array * @return an integer array */ vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { set<int> s1(nums1.begin(), nums1.end()), s2(nums2.begin(), nums2.end()), res; set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(res, res.begin())); return vector<int>(res.begin(), res.end()); } };
相关文章
- SPOJ220:Relevant Phrases of Annihilation(后缀数组)
- javaScript 数组对象取出某一列
- matlab学习笔记11_2高维数组操作 squeeze,ind2sub, sub2ind
- fork failed because of Out Of Memory
- 稀疏数组搜索
- 复盘:下面的这两个随机数组“a”和“b”:请问数组c=a+b的维度是多少
- 二维数组与二级指针
- JavaSE入门:数组
- JS Leetcode 852. 山脉数组的峰顶索引图解分析,高高的山峰一起吹山风吧。
- Arrays.toString(a)--->将数组a的值转换为字符串
- 转 数组 使用
- 力扣解法汇总2341. 数组能形成多少数对
- PHP实现删除一维数组中某一个值
- php数组array_push()和array_pop()以及array_shift()函数
- php 向二维数组中追加元素
- JS添加、更新和删除 JSON 数组
- 【数据结构】栈-数组的实现
- [LeetCode] 1248. Count Number of Nice Subarrays 统计优美子数组
- [LeetCode] 1005. Maximize Sum Of Array After K Negations K次取反后最大化的数组和
- [LeetCode] 977. Squares of a Sorted Array 有序数组的平方值
- [LeetCode] Maximum Sum of 3 Non-Overlapping Subarrays 三个非重叠子数组的最大和
- [LeetCode] Degree of an Array 数组的度
- [LeetCode] 34. Find First and Last Position of Element in Sorted Array 在有序数组中查找元素的第一个和最后一个位置