LeetCode : Intersection of Two Arrays

来源:互联网 发布:js点击按钮跳指定div 编辑:程序博客网 时间:2024/06/08 17:12

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.

class Solution {public:    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {       vector<int>result;       for(int i=0;i<nums1.size();++i)       {           if(find(nums2.begin(),nums2.end(),nums1[i])!=nums2.end())            {                if(find(result.begin(),result.end(),nums1[i])==result.end())                   {                       result.push_back(nums1[i]);                   }            }       }       return result;    }};
0 0