BinarySearch(Java)

 1     private int binarySearch(int[] input, int target) {
 2         if (input == null) {
 3             return -1;
 4         }
 5 
 6         int index1 = 0;
 7         int index2 = input.length-1;
 8         while (index1 <= index2) {
 9             int mid = (index1+index2)/2;
10             if (target == input[mid]) {
11                 return mid;
12             } else if(input[mid] < target) {
13                 index1 = mid+1;
14             } else {
15                 index2 = mid-1;
16             }
17         }
18 
19         return -1;
20     }

 

上一篇:56.两数之和.md


下一篇:C. 欧拉函数