[Sword refers to offer learning] Two numbers whose sum is a fixed value (expansion)

Source: Internet
Author: User
Next, the above article: http://blog.csdn.net/u013476464/article/details/40651451

Next, let's expand the question. Assume that the array is out of order, and that all elements in the array are non-negative integers. Similarly, given a number sum, we can find any two numbers in the array, make the sum of the two.

Analysis:


Because the elements in the array are defined as non-negative integers, we can use Hash ArrayOpen up a bool array B [Sum] with the length of sum, and initialize all the values to false to traverse array a once. If the current element a [I] is greater than sum, skip directly. Otherwise, continue to make the following judgment. If B [A [I] is false, Set B [Sum-A [I] to true, in this way, if B [A [I] is true, there are two numbers that meet the condition, they are a [I] and sum-A [I]. If no elements with B [A [I] being true are found after traversing a, it means that the elements meeting the condition cannot be found.

The time complexity of this algorithm is O (n), but the space complexity is O (SUM ). Or, if you know that the maximum value of the non-negative integer array a is Max, you can also open up a bool array of Max size. The idea is the same.


Hash Table introduction:

A hash table is a data structure that provides fast insert and delete operations. Regardless of the amount of data in the hash table, insertion or deletion only takes time close to the constant, that is, the time level of O (1. It is much faster than the tree. Tree operations usually require O (n) time.
Disadvantage: It is array-based and is difficult to maintain after the array is created. When some hash tables are basically filled up, the performance decline is very serious. In addition, there is no way to traverse data items in any order (for example, from large to small.
If you need to take the word as the key (array subscript) to get the value (data), you can break the word into a combination of letters, the letter into their digital code (A-1, B-2, C-3 ...... Z-26, space-27), multiply each number by the power of the corresponding 27 (because there are 27 possible letters, including spaces), then the result is added, each word corresponds to a unique number.


Code:

// Offer01.cpp: defines the entry point of the console application. // # Include "stdafx. H "# include" stdio. H "# include <stdlib. h> bool findnumsum (int * a, int Len, int sum, int * a, int * B) {if (a = NULL | Len <2) return false; bool * B = (bool *) calloc (sum, sizeof (bool); // bool array with the defined length of sum if (B = NULL) {exit (exit_failure) ;}for (INT I = 0; I <Len; I ++) {if (a [I]> = sum) continue; if (B [A [I] = false) B [Sum-A [I] = true; else {* A = A [I]; * B = sum-A [I]; free (B); // release Space B = NULL; return true ;}} free (B ); // release Space B = NULL; retur N false;} int main () {int N, K; static int A [1000000]; while (scanf ("% d", & N, & K )! = EOF) {int I; for (I = 0; I <n; I ++) {scanf ("% d", A + I);} int A, B; bool isfind = findnumsum (A, N, K, & A, & B); If (isfind) printf ("% d \ n", a, B ); elseprintf ("-1-1 \ n");} return 0 ;}


Running result:








[Sword refers to offer learning] Two numbers whose sum is a fixed value (expansion)

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.