Leetcode 350. Intersection of Arrays II

Source: Internet
Author: User

Given the arrays, write a function to compute their intersection.

Example: Given nums1 = [1, 2, 2, 1] , nums2 = [2, 2] , return [2, 2] .

Note:

    • Each element of the result should appear as many times as it shows in both arrays.
    • The result can be on any order.

Follow up:

    • What if the given array is already sorted? How would optimize your algorithm?
    • What if nums1' s size is small compared to num2' s size? Which algorithm is better?
    • What if elements of nums2 be stored on disk, and the memory is limited such so you cannot load all elements in To the memory at once?

Solution One:

1 classSolution {2  Public:3vector<int> Intersect (vector<int>& Nums1, vector<int>&nums2) {4vector<int>v;5         intLen2 =nums2.size ();6          for(inti =0; i < len2; i++)7         {8              for(intj =0; J < Nums1.size (); J + +)9             {Ten                  if(Nums2[i] = =Nums1[j]) One                  { A V.push_back (Nums2[i]); -Nums1.erase (Nums1.begin () +j); -                      Break; the                  } -             } -         } -         returnv; +     } -};
View Code

Solution Two:

1 classSolution {2  Public:3vector<int> Intersect (vector<int>& Nums1, vector<int>&nums2) {4vector<int>intersection;5 6         if(Nums1.empty () | |nums2.empty ())7         {8             returnintersection;9         }Ten  One sort (Nums1.begin (), Nums1.end ()); A sort (Nums2.begin (), Nums2.end ()); -  -  the         intm =nums1.size (); -         intn =nums2.size (); -  -         inti =0, j =0; +          while(I < m && J <N) -         { +                  if(Nums1[i] <Nums2[j]) A                  { ati++; -                  } -                  Else if(Nums1[i] >Nums2[j]) -                  { -J + +; -                  } in                  Else -                  { to Intersection.push_back (Nums1[i]); +i++; -J + +; the                  } *          } $ Panax Notoginseng  -         returnintersection; the     } +};
View Code

Solution three: With a hash table, simple rough, if it is ordered can not hash table, while scanning two arrays, find the same. If the NUMS2 is on disk, the hash table has no effect

1 classSolution {2  Public:3vector<int> Intersect (vector<int>& Nums1, vector<int>&nums2) {4         if(nums1.size () = =0|| Nums2.size () = =0)returnvector<int>();5vector<int>result;6unordered_map<int,int>Hash;7          for(Auto val:nums1) hash[val]++;8          for(auto Val:nums2)9         {Ten             if(Hash.count (val) && hash[val]>0) Result.push_back (val); Onehash[val]--; A         } -         returnresult; -     } the};
View Code

1. If not sorted, O (MN).
2. If M and n are within a reasonable range, first sort, then a comparison, time complexity O (nlgn + MLGM + m+n).
3. If M is far less than N, sort by N, M is also sorted (nlgn+mlgm+m+n), or M is not sorted (NLGN + mn). Both of these are the same. You can also do not sort m, binary search in N, so the complexity is reduced to NLGN+MLGN, down very low.
4. If n is large, n can only exist on disk. Can only put m load into memory, and then n one to read in, and M contrast, then m can be used hash, so the complexity of O (n).

Leetcode 350. Intersection of Arrays II

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.