In the given array, find the following table that first satisfies two numbers and equals the given number, and outputs the two elements.

Source: Internet
Author: User

Leetcode on a topic, although not difficult, but examined the data structure of a lot of knowledge

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

The function twosum should return indices of the numbers such that they add up to the target, where index1 must is Les S than Index2. Please note that your returned answers (both Index1 and INDEX2) is not zero-based.

You may assume this each input would has exactly one solution.

Input:numbers={2, 7, one, A, target=9
Output:index1=1, index2=2

1) Find two numbers and =target

The exhaustive method, which lists the double loops, finds the number smaller than the target starting from the first one, and the number of two numbers and =target after this number, so that the loop number of cycles is n+ (N-1) + (N-2) +...+1 = (n (n+1)), which is the complexity O (N2), Submit past found wrong, time out, it seems O (N2) complexity is too high

2) Find the number that satisfies target-numbers[i]

In this case, the common lookup algorithm is to find O (n) and Binary lookup O (log2n), for a lookup without an array, for each number[i], to see target-number[i] is not in the array, the complexity is O (N2), binary need to first sort to proceed, With a fast sorting algorithm (heap sort, quick sort, O (log2n))

Quick Sort : the time of fast sorting is mainly spent on the division operation, the interval of length k is divided, the total need k-1 the comparison of the key words.

The worst-case scenario is that each time the selected datum is the smallest (or largest) record of the keyword in the current unordered region, the result is that the sub-interval to the left of the Datum is empty (or the right sub-interval is empty), and the number of records in another non-empty sub-interval is divided by only one less than the number of records time complexity of O (n*n)

For fast sorting, its spatial complexity is O (LOGN), the worst case, the space complexity is O (n), you can use the pivot algorithm to reduce it to O (Logn) (that is, the basic ordered sequence of random selection of a datum point, or each time is left or the right sequence is empty)

In the best case, the datum for each partition is the "median" record of the current unordered region, and the result is that the length of the two unordered sub-ranges of the datum is roughly equal to the left and right. Total number of keyword comparisons: O (NLOG2N)

Although the worst time for fast sorting is O (N2), in terms of average performance, it is the fastest in the internal sorting algorithm based on the keyword comparison, and hence the name of the fast sort. Its average time complexity is O (NLGN).

Also attached is a sorted figure:

Back to the previous topic, that is, the first sort, in the selection, sorting algorithm complexity O (nlog2n), but fortunately only once, sorted well, you can do O (nlog2n) search, and finally O (nlog2n)

Another way is to build a hash, hash table to establish the time complexity of O (n), the complexity of the lookup is O (1), but requires O (n) space complexity, so this is a space-time method, sometimes very effective, and finally loads hash binary approach, making the complexity of O (n) +o ( LOG2N) is the space of the time +o (n) of O (n )

3) Find the sum of 22 numbers in numbers

The sum of the number 22 in an array, the complexity of which is undoubtedly O (n*n), but a different way of thinking, first sort, set two pointers to the sorted array to the team head tail, target large, then J forward, small then I move back, until find sum write down two subscript I and J

Scaling issues

1. What is your solution if you change the "two numbers" in this question to "three numbers" or "any number"?
Three numbers: First, the array is sorted, and then traversed from i=0 to n-1, traversing Arr[i], in the call to the function Getsumnum above (arr, sum-arr[i]).
An idea of any m number:
First, the array is sorted first, then from i=0 to n-1 element traversal, traversing arr[i], in the remaining n-1 elements called Getsumnum (Arr,sum-arr[i]), at this time for m-1 elements and Sum-arr[i]; Next, the same method , from j=0 to n-2 element traversal, traverse Arr[j], recursively call Getsumnum (Arr,sum-arr[i]-arr[j]) on arr, at this time for m-2 elements and sum-arr[i]-arr[j]; recursively, Until 2 elements are calculated and sum is-?-?-?... Until the time.
Whether it is to ask for 3 numbers is the m number, can always compare the poor lifting method less an order of N, than the first sort and then two-point find sum-arr[i] also fast.

In the given array, find the following table that first satisfies two numbers and equals the given number, and outputs the two elements.

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.