Intersection of Two Arrays II Solutions in C++
Number 350
Difficulty Easy
Acceptance 51.4%
Link LeetCode
Solutions
C++ solution by haoel/leetcode
// Source : https://leetcode.com/problems/intersection-of-two-arrays-ii/// Author : Calinescu Valentin, Hao Chen// Date : 2016-05-22/* Solution* --------** Follow up:** 1)If the given array is already sorted we can skip the sorting.** 2)If nums1 is significantly smaller than nums2 we can only sort nums1 and then binary* search every element of nums2 in nums1 with a total complexity of (MlogN) or if nums2* is already sorted we can search every element of nums1 in nums2 in O(NlogM)** 3)Just like 2), we can search for every element in nums2, thus having an online* algorithm.*/class Solution { // O(NlogN + MlogM)public:vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {sort(nums1.begin(), nums1.end());//we sort both vectors in order to intersectsort(nums2.begin(), nums2.end());//them later in O(N + M), where N = nums1.size()vector <int> solution; //M = nums2.size()int index = 0;bool finished = false;for(int i = 0; i < nums1.size() && !finished; i++){while(index < nums2.size() && nums1[i] > nums2[index])//we skip over theindex++;//smaller elements in nums2if(index == nums2.size())//we have reached the end of nums2 so we have no morefinished = true;//elements to add to the intersectionelse if(nums1[i] == nums2[index])//we found a common element{solution.push_back(nums1[i]);index++;}}return solution;}};/** Just simply use the map can have O(M+N) time complexity.**/class Solution {public:vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {unordered_map<int, int> m;for (auto n: nums1) {m[n]++;}vector<int> result;for (auto n:nums2){if (m.find(n) != m.end() && m[n]>0 ){result.push_back(n);m[n]--;}}return result;}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/intersection-of-two-arrays-ii/description//// Author : liuyubobobo/// Time : 2017-11-14#include <iostream>#include <vector>#include <unordered_map>using namespace std;/// Using Hash Map/// Time Complexity: O(len(nums1) + len(nums2))/// Space Complexity: O(len(nums1))class Solution {public:vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {unordered_map<int, int> record;for(int i = 0 ; i < nums1.size() ; i ++)record[nums1[i]] += 1;vector<int> resultVector;for(int i = 0 ; i < nums2.size() ; i ++)if(record[nums2[i]] > 0){resultVector.push_back(nums2[i]);record[nums2[i]] --;}return resultVector;}};void print_vec(const vector<int>& vec){for(int e: vec)cout << e << " ";cout << endl;}int main() {vector<int> nums1 = {1, 2, 2, 1};vector<int> nums2 = {2, 2};print_vec(Solution().intersect(nums1, nums2));return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/intersection-of-two-arrays-ii/description//// Author : liuyubobobo/// Time : 2017-11-14#include <iostream>#include <vector>#include <set>using namespace std;/// Using Hash Map/// Time Complexity: O(len(nums1) + len(nums2)*log(len(nums1)))/// Space Complexity: O(len(nums1))class Solution {public:vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {multiset<int> record;for(int num: nums1)record.insert(num);multiseSt<int> result;for(int num: nums2){multiset<int>::iterator iter = record.find(num);if( iter != record.end()){result.insert(num);record.erase(iter);}}return vector<int>(result.begin(), result.end());}};void print_vec(const vector<int>& vec){for(int e: vec)cout << e << " ";cout << endl;}int main() {vector<int> nums1 = {1, 2, 2, 1};vector<int> nums2 = {2, 2};print_vec(Solution().intersect(nums1, nums2));return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/intersection-of-two-arrays-ii/description//// Author : liuyubobobo/// Time : 2019-04-08#include <iostream>#include <vector>#include <set>using namespace std;/// Sorting and Two Pointers/// Time Complexity: O(nlogn)/// Space Complexity: O(1)class Solution {public:vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {sort(nums1.begin(), nums1.end());sort(nums2.begin(), nums2.end());vector<int> res;int i = 0, j = 0;while(i < nums1.size() && j < nums2.size()){if(nums1[i] == nums2[j]) res.push_back(nums1[i]), i ++, j ++;else if(nums1[i] < nums2[j]) i ++;else j ++;}return res;}};void print_vec(const vector<int>& vec){for(int e: vec)cout << e << " ";cout << endl;}int main() {vector<int> nums1 = {1, 2, 2, 1};vector<int> nums2 = {2, 2};print_vec(Solution().intersect(nums1, nums2));return 0;}