[LeetCode] 349 Intersection of Two Arrays & 350 Intersection of Two Arrays II
這兩道題都是求兩個數組之間的重復元素,因此把它們放在一起。
原題地址:
349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/
350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/
題目&解法:
1.Intersection of Two Arrays:
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.
這道題目要註意的就是不能重復。我采取的做法是遍歷一遍nums1數組,然後和nums2數組比對,假如nums2裏面存在並且要返回的數組中沒有這個數值,就把他插入要返回的數組裏面。很低端的一種做法,代碼如下:
class Solution { public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { vector<int> temp; for (int i = 0; i < nums1.size(); i++) { if (find(nums2.begin(), nums2.end(), nums1[i]) != nums2.end() && find(temp.begin(), temp.end(), nums1[i]) == temp.end()) { temp.push_back(nums1[i]); } }return temp; } };
2.Intersection of Two Arrays II
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?
這道題目比上面的題目復雜了一點,它要求把重復的元素都放進返回的數組裏面,我采取了一種非常非常垃圾的做法:先定義一個結構體,一個int類型和一個bool類型,int變量數值復制傳入的數組,然後用bool變量標記當前元素的數值是否已經插入要返回的數組。然後采取雙層循環逐個比對。代碼如下:
class Solution { public: vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { struct v{ int data; bool isChoosed; }; vector<struct v> struct_num1; vector<struct v> struct_num2; for (int i = 0; i < nums1.size(); i++) { struct v t; t.data = nums1[i]; t.isChoosed = false; struct_num1.push_back(t); } for (int i = 0; i < nums2.size(); i++) { struct v t; t.data = nums2[i]; t.isChoosed = false; struct_num2.push_back(t); } vector<int> temp; for (int i = 0; i < nums1.size(); i++) { for (int j = 0; j < nums2.size(); j++) { if (struct_num1[i].data == struct_num2[j].data && struct_num2[j].isChoosed == false && struct_num1[i].isChoosed == false) { temp.push_back(struct_num2[j].data); struct_num1[i].isChoosed = true; struct_num2[j].isChoosed = true; } } } return temp; } };
這種做法讓我鄙視我自己,時間復雜度為O(n^2),極高。而且寫起來極其麻煩。肯定有簡單的方法啊!
根據http://blog.csdn.net/yzhang6_10/article/details/51526070裏面的一種比較快的思路:
(1)先對兩個數組進行排序
(2)遍歷兩個數組,比較對應元素:若相等,兩個數組的索引同時增加;若不等,較小元素的數組的索引增加。
這是一個極精妙的方法,個人感覺原理和歸並數組有點相似。這個算法值得經常去回顧一下,特此記錄。
代碼如下:
class Solution { public: vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { sort(nums1.begin(), nums1.end()); sort(nums2.begin(), nums2.end()); vector<int> result; for (int i = 0, j = 0; i < nums1.size() && j < nums2.size(); ) { if (nums1[i] == nums2[j]) { result.push_back(nums1[i]); i++; j++; } else if (nums1[i] < nums2[j]) i++; else if (nums1[i] > nums2[j]) j++; } return result; } };
[LeetCode] 349 Intersection of Two Arrays & 350 Intersection of Two Arrays II