[Algorithm question] Find two numbers in the array so that the sum is equal to a certain number.

Source: Internet
Author: User
Tags map data structure

From: http://leetcode.com/onlinejudge

Two sum

Given an array of integers, find two numbers such that they add up to a specific target number.

The function twosum shocould return indices of the two numbers such that they add up to the target, where index1 must be less than index2. please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input wowould have exactly one solution.
Input: Numbers = {2, 7, 11, 15}, target = 9

Output: index1 = 1, index2 = 2


Today, we are at least (N ^ 2 ).

Obviously, this solution is not the best answer to this question. I hope to give a better solution to time complexity. In programming beauty 2.12, I explained this question, different time complexity solutions are provided.

  1. O (N * log (N) solution: first sort the Array (time complexity is O (n * log (N ))), in the sorted array, check whether target-Arr [I] is in the array, when searching for target-Arr [I], you can use binary search (the time complexity is O (log (N ))), traverse the array and find the target-Arr [I] in the array. Therefore, the search time complexity is O (n * log (n )), the total time complexity is O (n * log (n )).
  2. Another solution of O (N * log (N): solution 3 given in "the beauty of programming" is more interesting than solution 1, this mainly improves the search process. First, sort the Array (the time complexity is O (n * log (N), and then make the I = 0, j = n-1, judge whether arr [I] + arr [J] is equal to target, equals ends, if less than target, then I = I + 1; if greater than target, j = J-1, in this way, the process of searching two elements that meet the conditions only needs to traverse the array once, so the time complexity of searching is reduced to O (N ), the total time complexity is O (n * log (n )).
  3. O (n) solution: store all elements in the array into the hash table, traverse the time complexity O (n), and then traverse the array again, query whether target-Arr [I] appears in the hash table. Because the Query Process in the hash table only takes O (1) time, the time complexity of the second traversal is O (n ), the total time complexity is O (n ). However, this method also has disadvantages (also the inherent disadvantage of hash tables), that is, it has a high space complexity-the hash table storage space that requires O (N), such as {1, 3, 6, 100000} such an array requires of space for storage. The storage efficiency is too low and too low! In addition, because only the number of tags has been used, the bitmap data structure (see:) can be used to save space. Hash table is a method for changing the time of space. Therefore, you need to select an appropriate method based on the actual situation.
In addition, the following code is provided by one of http://www.dewen.org/q/6111:
# Include <iostream> # include <map> using namespace STD; int main () {int A [] = {23,456, 34 }; // array Map <int, int> m; int sum = 10; // The given integer int COUNT = sizeof (a)/sizeof (INT ); for (INT I = 0; I <count; I ++) {M. insert (Map <int, int >:: value_type (Sum-A [I], a [I]); // put it in hash map} For (INT I = 0; I <count; I ++) {If (M. find (A [I])-> second = sum-A [I]) cout <"find" <I <Endl; // search for the hash value} return 0;} c ++ approximate The implementation is like this.

Obviously, he wants to use the hash table method in the preceding 3, but here the map data structure in STL is used, while the map underlying layer in STL is implemented by the red/black tree, so is the time complexity of searching in map O (log (N? So is the actual time complexity of this program O (N * log (N?

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.