The problem of the sum of two numbers (find the two elements in the array as the two elements of the given integer)

Problem: Enter an integer x containing n integers that is an array A, find i and j that satisfy A [i] + A [j] = x, and if there is no such element sum, return -1 and -1.

 

My analysis is as follows:

This question needs to be noted: there may be duplicate elements in the array, and there are more than one set of solutions that meet the conditions .

For example, given the array A [1,3,2,2] and x = 4, the solution that satisfies the condition is

(0, 1): 1 + 3 = 4 and (2, 3): 2 + 2 = 4.

 

Solution 1: Violent solution

Iterate over each element A [i] and find if there is a target element with a value equal to xA [i].

Test cases and running results:

Solution 2: Sort first + pointer + HashMap

  Sort the given array first, and then create two pointers (i, j), one from the beginning node to the back of i ++, and one from the tail node to the front of j--;

 There are three cases:

int i = 0, j = A.length;

      while(i < j) {

          if(A[i]+A[j] < x) 

             i++;

          else if(A[i]+A[j] > x) 

             j--;

          else

             System.out.print("find out!");

    }

   However, the index of the array will be confused because of the reordering. Therefore, the original array elements and their indexes can be stored in the hash table before sorting. After the elements are found out with the pointer, the original index is taken out through the hash table.

Time complexity analysis, the access hash table is O (n), the order is O (nlog (n)), and the pointer search is O (n), which is about O (2n + nlog (n)).

Solution 3: Hash table

hm: Store the elements and indexes in the array in the form of <key, value> in the hash table hm, (gradually optimized from traversing the hash table twice to traversing the hash table once)

(1) The first thought is to traverse the hash table twice: f (n) = 2n

         * The first time is to store each element in the array and its index in a hash table in the form of key-value pairs

         * The second time is to traverse each element A [i] in turn to see if xA [i] is a key in the hash table. If it exists, the key value is the index.

But the problem with this situation is: because the traversal will output the solution twice, such as (1, 2) and (2, 1).

(2) The above two traversal hash tables can be changed into one traversal f (n) = n, that is, before adding the array element a [i] to the hash table, first determine whether xa [i] is already in the current hash In the table, and then add, so that there will be no duplicate output solution.

     * Hash table trades space for time, space complexity is O (n), time complexity is O (n)

     * Disadvantages: Duplicate keys are not allowed in the hash table, so this algorithm cannot solve the situation of the same element in the array.

Note: Some problems will tell that there is only one pair of such solutions. This situation is relatively simple. You do not need to consider repeated elements and you can end the program as long as you find the solution.

Published 61 original articles · 61 praises · 20,000+ views

Guess you like

Origin blog.csdn.net/qq_42475914/article/details/104743872