题解 | #二分查找#
二分查找
http://www.nowcoder.com/practice/28d5a9b7fc0b4a078c9a6d59830fb9b9
import java.util.*;
public class BinarySearch {
public int getPos(int[] A, int n, int val) {
// write code here
int low=0,higth=n,res=-1;
while (low != higth){
int mid = (low + higth)/2;
if (A[mid] > val){
higth = mid;
}else if (A[mid] < val){
low = mid;
if (res != -1){
break;
}
}else {
res = mid;
higth = mid;
}
}
return res;
}
}