Given three integer arrays arr1, arr2 and arr3 sorted in strictly increasing order, return a sorted array of only the integers that appeared in all three arrays.
Example 1:
Input: arr1 = [1,2,3,4,5], arr2 = [1,2,5,7,9], arr3 = [1,3,4,5,8] Output: [1,5] Explanation: Only 1 and 5 appeared in the three arrays.
Constraints:
-
1 <= arr1.length, arr2.length, arr3.length <= 1000
-
1 <= arr1[i], arr2[i], arr3[i] <= 2000
** USE HASH MAP
class Solution {
public:
vector arraysIntersection(vector& arr1, vector& arr2, vector& arr3) {
map<int, int> hmap;
for(int i = 0; i < arr1.size(); i++)
{
hmap[arr1.at(i)] = 1;
}
for(int i = 0; i < arr2.size(); i++)
{
int each = arr2.at(i);
if(hmap.find(each) != hmap.end())
{
hmap[each]++;
}
}
for(int i = 0; i < arr3.size(); i++)
{
int each = arr3.at(i);
if(hmap.find(each) != hmap.end())
{
hmap[each]++;
}
}
vector res;
map<int, int>::iterator iter;
for(iter = hmap.begin(); iter != hmap.end(); iter++)
{
if(iter->second == 3)
{
res.push_back(iter->first);
}
}
return res;
}
};
'Computer General > Algorithm and Data Structure' 카테고리의 다른 글
Palindrome Permutation (0) | 2020.03.21 |
---|---|
Moving Average from Data Stream (0) | 2020.03.21 |
Nested List Weight Sum (0) | 2020.03.21 |