Sorting and comparing, C++, 10 ms

  • 1
    class Solution {
        vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
            int n=nums1.size(); int m=nums2.size();vector<int> re;
            if(n==0||m==0) {return re;}
            sort(nums1.begin(), nums1.end());
            sort(nums2.begin(), nums2.end());
            int i=0; int j=0;
                if (nums1[i]==nums2[j]){
                    re.push_back(nums1[i]); i++;j++;}
                else if(nums1[i]<nums2[j])i++;
                else j++;
            }return re; }

Log in to reply

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