Ready to be perfected!
Reprint Please specify source: http://www.cnblogs.com/wuzetiandaren/p/4259199.html
Statement: Most of the articles for the search for problems in the online reprint, this Bo is to make a record for themselves, convenient for their friends with similar problems, the idea of this article may have some reference, but the source code are for me to achieve, if there is infringement, please send an email to indicate the article and the original source address, I must indicate in the article Thank you.
Topic:
Enter an array and a number that has been sorted in ascending order,
Find two numbers in the array so that they are exactly the number entered.
The required time complexity is O (n). If there are many pairs of numbers and equals the input number, the output can be any pair.
For example, input arrays 1, 2, 4, 7, 11, 15, and number 15. Because of the 4+11=15, the outputs are 4 and 11.
I think the output subscript is more reasonable.
Learn the idea of quick sequencing:
(1) Let the pointer point to the head and tail of the array, add if it is less than m, increment the pointer, or decrease the tail pointer if it is greater than
(2) Condition of exit, equal or head = Tail
Java Implementation Source code:
1 PackageCom.interview;2 3 ImportJava.util.HashMap;4 /**5 * Title: Enter an array that has been sorted in ascending order and a number, looking for two numbers in the array,6 * make their and exactly the number of inputs. The required time complexity is O (n). 7 * @authorWjh8 *9 */Ten Public class_14ascsortsum { One A /** - * @paramargs - */ the Public Static voidMain (string[] args) { - // -_14ascsortsum sum =New_14ascsortsum (); - int[] A = { -3,-2,0,1,2,3,6,7,9,15,17,19}; +hashmap<integer,integer> map =NewHashmap<integer,integer>(); -Map = Sum.searchsum (A,9,MAP);//1) Find +Sum.printresult (A,MAP);//Print Find Results A } at - - //find, subscript starting from 0 - PrivateHashmap<integer,integer> Searchsum (int[] A,intNum, hashmap<integer,integer>map) { - intn =a.length; - inti=0,j=n-1;//I and J Save the current small and big data, respectively . in //map map = new hashmap<integer,integer> (); - while(I<J) {//Find Criteria to while(I<j && (A[i]+a[j]) >Num) { +j--; - } the if(i<j&& (a[i]+a[j]) = =Num) { * Map.put (i, j); $i++;Panax Notoginseng //return map; //If you find it, end it, add it. - } the while(I<j && (A[i]+a[j]) <Num) { +i++; A } the if(i<j&& (a[i]+a[j]) = =Num) { + Map.put (i, j); -j--; $ //return map; //If you find it, end it, add it. $ } - } - returnmap; the } - Wuyi //Print Find Results the Private voidPrintresult (int[] A, hashmap<integer,integer>map) { - intn =map.size (); Wu if(n==0){ -System.out.println ("Not Found! "); About return; $ } -System.out.println ("This is all the elements found:"); - for(Integer key:map.keySet ()) { -System.out.println ("Subscript is: (" +key+ "," +map.get (key) + "), the value is:" +a[key]+ "," +A[map.get (key)]); A } + } the -}
Full Source
1 PackageCom.interview;2 3 ImportJava.util.HashMap;4 /**5 * Title: Enter an array that has been sorted in ascending order and a number, looking for two numbers in the array,6 * make their and exactly the number of inputs. The required time complexity is O (n). 7 * @authorWjh8 *9 */Ten Public class_14ascsortsum { One A /** - * @paramargs - */ the Public Static voidMain (string[] args) { - // -_14ascsortsum sum =New_14ascsortsum (); - int[] A = { -3,-2,0,1,2,3,6,7,9,15,17,19}; +hashmap<integer,integer> map =NewHashmap<integer,integer>(); -Map = Sum.searchsum (A,9,MAP);//1) Find +Sum.printresult (A,MAP);//Print Find Results A } at - - //find, subscript starting from 0 - PrivateHashmap<integer,integer> Searchsum (int[] A,intNum, hashmap<integer,integer>map) { - intn =a.length; - inti=0,j=n-1;//I and J Save the current small and big data, respectively . in //map map = new hashmap<integer,integer> (); - while(I<J) {//Find Criteria to while(I<j && (A[i]+a[j]) >Num) { +j--; - } the if(i<j&& (a[i]+a[j]) = =Num) { * Map.put (i, j); $i++;Panax Notoginseng //return map; //If you find it, end it, add it. - } the while(I<j && (A[i]+a[j]) <Num) { +i++; A } the if(i<j&& (a[i]+a[j]) = =Num) { + Map.put (i, j); -j--; $ //return map; //If you find it, end it, add it. $ } - } - returnmap; the } - Wuyi //Print Find Results the Private voidPrintresult (int[] A, hashmap<integer,integer>map) { - intn =map.size (); Wu if(n==0){ -System.out.println ("Not Found! "); About return; $ } -System.out.println ("This is all the elements found:"); - for(Integer key:map.keySet ()) { -System.out.println ("Subscript is: (" +key+ "," +map.get (key) + "), the value is:" +a[key]+ "," +A[map.get (key)]); A } + } the -}
Operation Result:
Here are all the elements found:
Subscript is: (2,8), Value: 0,9
Subscript is: (4,7), Value: 2,7
Subscript is: (5,6), Value: 3,6
14. Enter an array and a number that have been sorted in ascending order, and find two numbers in the array so that they are exactly the number entered. Requires a time complexity of O (n)