1. The sum of two
Given an array of integers and a target value, find the two numbers in the array and the target values.
You can assume that each input corresponds to only one answer, and that the same element cannot be reused.
Example :
given nums = [2, 7, one, all], target = 9
because nums[0] + nums[1] = 2 + 7 = 9
so return [0, 1]
Solution Solutions
Method One: Violence law
The law of violence is simple. Iterate through each element X and find out if there is a target element with a value equal to target.
1 //Violence Law2 classSolution {3 Public:4vector<int> Twosum (vector<int>& Nums,inttarget) {5 intSize =nums.size ();6 7 for(intI=0; i<size-1;++i)8 for(intj = i+1; J < size; ++j)9 {Ten if(Nums[i] + nums[j] = =target) One returnvector<int>{i, J}; A } - returnvector<int> {0,0}; - } the};
Analysis of Complexity:
L time complexity:O (n^2), for each element, we try to find its corresponding target element by traversing the remainder of the array, which consumes O (n) time. So the time complexity is O (n^2).
L Spatial Complexity:O (1).
Run Time:
96ms
Method two : A hash table again
It turns out that we can do it at once. While iterating and inserting elements into the table, we also look back to see if the target element for the current element already exists in the table. If it exists, then we have found the corresponding solution and immediately return it.
1 //Optimal Solution2 //use a hash table for one traversal3 classSolution {4 Public:5vector<int> Twosum (vector<int>& Nums,inttarget) {6 intSize =nums.size ();7map<int,int>Map;8 9Map.insert (Make_pair (target-nums[0],0));Ten for(inti =1; i < size; ++i) One { Amap<int,int>::iterator iter =Map.find (Nums[i]); - if(ITER! =map.end ()) - { the intindex = iter->second; - if(Index! =i) - returnvector<int>{index,i}; - } +Map.insert (Make_pair (Target-Nums[i], i)); - } + returnvector<int>{0,0}; A } at};
Analysis of Complexity:
L Time Complexity:O (n), we only traverse the list containing the elements of nn once. Each lookup that is made in a table takes only O (1) of time.
L Spatial Complexity:O (N), the additional space required depends on the number of elements stored in the hash table, which requires a maximum of n elements to be stored.
Run Time:
8ms
Add
C + + Map considerations:
1, in the map, when the key to find value, the first to determine whether the map contains a key.
2. If you do not check and return directly to Map[key], unexpected behavior may occur. If the map contains key, there is no problem, if the map does not contain a key, using the subscript has a dangerous side effect, will insert a key element in the map, value takes the default value, return value. In other words, Map[key] cannot return null.
3, Map provides two ways to see if it contains Key,m.count (key), M.find (key).
4, M.count (key): Since the map does not contain duplicate keys, M.count (key) has a value of 0, or 1, indicating whether it is included.
5. M.find (Key): Returns an iterator to determine whether it exists.
6, for the following scenario, there is a key to use, otherwise return NULL, there are the following two kinds of wording:
if (m. Count(key) >0) { return m[key]; } return null; |
Iter = m. Find(key); if (iter! =m. End()) { return iter-second; } return null; |
Note here: The previous method is intuitive, but inefficient. Because of the previous method, you need to perform a two-time lookup. Therefore, it is recommended to use the latter method.
7, for the container in the STL, there is a generic algorithm find (begin,end,target) search target, map also provides a member method find (Key)
1. The sum of two