c++ simple solution


  • 0
    Y

    #include<algorithm>
    class Solution {
    public:
    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
    vector<int> s;
    if(nums1.size()==0||nums2.size()==0){return s;}
    sort(nums1.begin(),nums1.end());
    sort(nums2.begin(),nums2.end());

        for(int i=0,j=0;i<nums1.size()&&j<nums2.size();){
            if(nums1[i]<nums2[j]){
                i++;
            }
            else if(nums1[i]>nums2[j]){
                j++;
            }
            else {
                s.push_back(nums1[i]);
                i++;
                j++;}
        }
        return s;
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.