349. Intersection of Two Arrays

Problem

Given two arrays, write a function to compute their intersection.
Note:

  • Each element in the result must be unique.
  • The result can be in any order

Example

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [9,4]

Code

static int var = [](){
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}();
class Solution {
public:
    vector intersection(vector& nums1, vector& nums2) {
        sort(nums1.begin(),nums1.end());
        nums1.erase(unique(nums1.begin(),nums1.end()),nums1.end());
        sort(nums2.begin(),nums2.end());
        nums2.erase(unique(nums2.begin(),nums2.end()),nums2.end());
        int i=0,j=0;
        vector res;
        while(i

Result

349. Intersection of Two Arrays.png

你可能感兴趣的:(349. Intersection of Two Arrays)