Leetcode Brush Question (a): The Sum of the

Source: Internet
Author: User
Tags map class

Today began in Leetcode Brush topic, the first is titled "Two Number of sum (two sums)", the overall problem is relatively low, but still encountered some problems in the process, the following major records of some problems.

This topic is easier to think of the method is poor lifting, very violent but also very direct, the need to pay attention to the vector library vectors, oneself is also a few years ago began to learn the data structure to know that there is this library (some also known as a container), the definition of the calculation of the length of the array is size () rather than length The official answer to is length (), do not know whether it is not noticed or because he used the code is Java (I do not know Java), the rest no longer say.

It's the only way I can think of.-_-| | So read the official puzzle, only to know that there is a hash table this good thing, the basic principle I picked up the online quote: Use a subscript range of larger arrays to store elements. You can design a function (a hash function, also called a hash function) so that each element's keyword corresponds to a function value (that is, the array subscript, hash value), and then use the array unit to store the element, or it can be simply understood, by the keyword for each element "classification", This element is then stored in the corresponding "class" corresponding to the place, called the bucket. However, it is not possible to guarantee that each element's keyword corresponds to a function value of one by one, so it is very likely that there are different elements, but the same function values are computed, thus creating a "conflict", in other words, by splitting different elements into the same "class". In general, "direct addressing" and "conflict resolution" are two major features of the hash table. (---------------------principle This passage comes from the CSDN blog of Jian Xi Lou, the full text address please click: 52416583?utm_source=copy)

A hash table is a classic example of a trade-off between time and space. If there is no memory limit, you can directly index the key as an array. Then all the lookup time complexity is O (1), and if there is no time limit, then we can use unordered arrays and order lookups, which requires very little memory. The hash table uses a modest amount of time and space to find a balance between these two extremes. You only need to adjust the hash function algorithm to make time and space choices. (---------------------example this passage from sam5828 's CSDN blog, full-text address click: 52885030?utm_source=copy).

After viewing some of the information on the Internet, like a hash table and a dictionary similar to the use of key-value pairs to store data, then understand the principle of learning hash_map methods, get started! First, according to the "two times hash Table" idea code code, according to the compilation result hint to the code to continuously rewrite, finally did not succeed, took the great effort finally to see in the Visual Studio Help document the Hash_map already was a past style (heart 10,000 head lovely alpaca rushes over ...) , now replaced with unordered_map, and finally I chose the map class, at that time did not figure out what the difference between the two, just a rough understanding of their two methods are almost the same, need to pay attention to the difference between the at method and the Find method, the former returns the value of the key, The latter returns an iterator that references the position of the element in the map that has the key equivalent to the specified key. Complete the hash table after two times the hash table is done naturally.

Comparison of three methods:

Poor Lifting method Two times hash table Over the hash table
Complexity of Time O (N2) O (n) [hash table lookup element time is O (1), Time to build is O (n) O (N)
Complexity of space O (1) O (n) [additional space created by the hash table depends on the number of elements] O (N)

C + + code paste below:

1 //Poor Lifting Method2 classSolution {3  Public:4vector<int> Twosum (vector<int>& Nums,inttarget) {5          for(inti =0; I < nums.size (); i++) 6         {7              for(intj = i +1; J < Nums.size (); J + +) 8             {9                 if(Nums[j] = = Target-Nums[i])Ten                 { One                     return{i, J}; A                 } -             } -         } the     } - }; -  - //two times hash table + classSolution { -  Public: +vector<int> Twosum (vector<int>& Nums,inttarget) { Amap<int,int>TMap;  at          for(inti =0; I < nums.size (); i++)   - Tmap.insert (Make_pair (nums[i], i)); -          for(inti =0; I < nums.size (); i++) -         { -             intj = Target-Nums[i]; -             if(Tmap.count (j) = =1&& tmap.at (j)! =i) in                 return{i, tmap.at (j)}; -         } toprintf"there is no and sum solution."); +     } - }; the  * //over the hash table $ classSolution {Panax Notoginseng  Public: -vector<int> Twosum (vector<int>& Nums,inttarget) { theunordered_map<int,int>TMap;  +          for(inti =0; I < nums.size (); i++)   A         { the Tmap.insert (Make_pair (nums[i], i)); +             intj = Target-Nums[i]; -             if(Tmap.count (j) = =1&& tmap.at (j)! =i) $                 return{tmap.at (j), i};//Note that when a conditional element is found, the position of tmap.at (j) must be in front of I, so the output is reversed in order $              -         } -printf"there is no and sum solution."); the     } -};

Leetcode Brush Question (a): The Sum of the

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.