Directory
1 Problem Description
2 Solutions
2.1 Sorting Clamp forcing method
1 problem description
enter an array of integers and an integer to find two numbers in the array that meet their and exactly the input integers. If there are multiple logarithm and equal to the input integer, the output can be any pair. For example, if you input an array [1,2,4,5,7,11,15] and an integer , then theoutput is 4 because 4+11 = and the 11.
2 Solutions 2.1 Sorting Clamp forcing method
First, an array of integers, using a merge sort, is used to sort from the beginning to the middle, and then the ordered array is traversed from both ends, and once the two-digit and equal-input integer is present, the two numbers are immediately output and the traversal is ended.
The specific code is as follows:
Packagecom.liuzhen.array_2; Public classTwosumn {/** Parameter A: Given a small to large sorted array * parameter n: to sum n * Function function: Print out A of two elements, meet a[i]+a[j] = n*/ Public voidGettwosumn (int[] A,intN) { intStart = 0; intEnd = A.length-1; while(Start <end) { if(A[start] + a[end] = =N) {System.out.println ("\ n array elements a[" +start+ "]" + "+ a[" +end+ "] =" +n+ ", a[" +start+ "] =" +a[start]+ ", a[" +end+ "] =" +A[end]); Break; } Else{ if(A[start] + a[end] >N) End--; ElseStart++; } } } //Merge Sort Public voidMergeSort (int[] A) { if(A.length > 1){ int[] Lefta = Gethalfarray (a,0);//the left half of the array a int[] Righta = Gethalfarray (a,1);//right half of array amergesort (LEFTA); MergeSort (Righta); Getmerge (A,lefta,righta); } } /** Parameter A: The array to be binary * parameter Judge:judge = = 0 to return the upper left half of the array, Judge! = 0 means returning the right half of the array a function function: The array is divided by length into the upper half and Lower half*/ Public int[] Gethalfarray (int[] A,intjudge) { int[] result; if(Judge = = 0) {result=New int[A.LENGTH/2]; for(inti = 0;i < a.length/2;i++) Result[i]=A[i]; } Else{result=New int[A.LENGTH-A.LENGTH/2]; for(inti = 0;i < a.length-a.length/2;i++) Result[i]= A[I+A.LENGTH/2]; } returnresult; } /** Parameter A: Given the array to be sorted * parameter LEFTA: The left half of the array A * parameter Righta: The right half of the array * Function function: Returns the small to large sort of array a*/ Public voidGetmerge (int[] A,int[] Lefta,int[] righta) { inti = 0;//number of elements used to calculate the current traversal Lefta intj = 0;//number of elements used to calculate the current traversal Righta intCount = 0;//used to calculate the number of elements of a that are currently sorted by small to large while(I < lefta.length && J <righta.length) { if(Lefta[i] <Righta[j]) {A[count++] =Lefta[i]; I++; } Else{A[count++] =Righta[j]; J++; } } if(I <lefta.length) { while(I <lefta.length) A[count+ +] = lefta[i++]; } if(J <righta.length) { while(J <righta.length) A[count+ +] = righta[j++]; } } Public Static voidMain (string[] args) {twosumn test=NewTwosumn (); int[] A = {2,1,7,4,6,1,2,4,3,6,8,4,2,1,7,3,4,6,8,3,4}; Test.mergesort (A); System.out.println ("The result of merging sorted array A:"); for(inti = 0;i < a.length;i++) System.out.print (A[i]+" "); Test.gettwosumn (A,10); }}
operation result ;
after merging the sorted array A results:1 1 1 2 2 2 3 3 3 4 4 4 4 4 6 6 6 7 7 8 8 Arrays of elements a[3] + a[20] = 10,a[3] = 2,a[20] = 8
Algorithm Note _037: Search and two numbers for the fixed value (Java)