二分查找
二分查找算法:
二分查找求下界:
class BinarySearch { public: int getPos(vector<int> A, int n, int val) { // write code here int start=0, end=n; while(start < end){ int mid = (start + end)/2; if(A[mid] < val) start = mid +1; else end = mid ; } if(end == n || A[end] != val) return -1; else return end; } };
二分查找求上界:
posted on 2015-09-20 20:44 gaoyanqing 阅读(152) 评论(0) 收藏 举报
浙公网安备 33010602011771号