Reimplement C++ STL lower_bound
class Solution {
public:
int searchInsert(int A[], int n, int target) {
return lower_bound(A, 0, n-1, target);
}
int lower_bound(int A[], int first, int last, int target)
{
while(first <= last)
{
int mid = first + (last-first)/2;
if(A[mid] < target)
first = mid + 1;
else if(A[mid] >= target)
last = mid - 1;
}
return last+1;
}
};
Reference: Leetcode "Search for a Range"