How fast my code run?O(?)


  • 0
    E
      vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        std::sort(nums1.begin(), nums1.end());
        std::sort(nums2.begin(), nums2.end());
        vector<int> ret;
        
        auto temp = nums2.begin();
        for(auto beg1 = nums1.begin(); beg1 != nums1.end(); ++beg1)
            {
                if(beg1 != nums1.begin() && *(beg1 - 1) == *beg1) continue;
                for(auto beg2 = temp; beg2 != nums2.end() && *beg2 <= *beg1; ++beg2)
                    if(*beg1 == *beg2) 
                    {
                        temp = beg2;
                        ret.push_back(*beg2);
                        break;
                    }
            }
        return ret;
            
        
    }

  • 0

    It is O(n log n + m log m). m and n are the length of two arrays.


Log in to reply
 

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