350-- two array elements returned CROSS

1. This question and 349 difference is that, not only to return to repeat the numbers, but also to ensure duplicate numbers appear several times to return to

2, on a question so I contacted the set container, this question let me come into contact with unordered_map container (you know the difference between map and unordered_map !!!), based on the hash table

Then the difference in the link: https: //blog.csdn.net/u013130743/article/details/80794177 (reprint author of csdn summary)

. 3, Map container and unordered_map basic usage

#include<map>;

unordered_map <int, string> map1; // is the first key, the second key value is stored in the object (which can be string, int may be other types of data)

map1.insert (pair <int, string>) (1, "zhangsan"); // insert mode three data

map1.insert(map<int,string>::value_type)(1,"zhangsan");

map1 [1] = "zhangsan"; // this can cover the insert mode, not the above two

map1.empty (); // determines whether the air

map1.begin (); map1.end (); // iteration start and end positions

map1.size (); // returns the number of elements

map1.count (); // Returns the number of times specified element appears

map1.[key];

4, thinking

Define a ordered_map, underlying hash table; vector defines a container mounted repeating elements

The element of the array and a number of occurrences stored in the map, traversing the second array, a map to find a corresponding number minus 1 occurs while the elements stored in a vector

5, Code

1  // first contact map associated with the container
 2  // previous question contact is set, check to see information and set different from that map, map key has no value set 
. 3  
. 4  class Solution {
 . 5  public :
 . 6      Vector < int > INTERSECT (Vector < int > & nums1, Vector < int > & nums2) {
 . 7          Vector < int > REC;
 . 8          unordered_map < int , int > MAP1;
 . 9          for ( int I = 0 ; I <nums1.size (); I ++ ) {
 10             map1[nums1[i]]+=1;
11         }
12         for(int i=0;i<nums2.size();i++){
13             if(map1[nums2[i]]>0) {
14                 rec.push_back(nums2[i]);
15                 map1[nums2[i]]-=1;
16             }
17         }
18         return rec;
19     }
20 };

 

Guess you like

Origin www.cnblogs.com/hehesunshine/p/11649437.html