</pre><p>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.这个题目的算法很简单: 首先,将两个数组的相同数字存到一个临时向量中; 然后,将临时向量中重复的数字去掉并将结果存到要返回的向量中。</p><p></p><p></p><pre name="code" class="cpp">class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
vector<int> temp;
for(int i=0;i<nums1.size();i++)
{
for(int j=0;j<nums2.size();j++)
{//cout <<i<<" "<<j<<" "<< "Hello world!" << endl;
if(nums2.at(j)==nums1.at(i))
temp.push_back(nums2.at(j));
}
}
vector<int>result;
for(int i=0;i<temp.size();i++)
{
int flag=0;
for(int j=i+1;j<temp.size();j++)
{
if(temp.at(i)==temp.at(j))
flag=1;
}
if(flag==0)
result.push_back(temp.at(i));
}
return result;
}
};
sky_Intersection of Two Arrays
最新推荐文章于 2023-03-06 20:18:39 发布