Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume NO duplicates in the array.
http://www.lintcode.com/en/problem/search-insert-position/#
int searchInsert(vector<int> &A, int target) {
int n = A.size();
if(n==0) return 0;
int start = 0;
int end = n-1;
while(start+1 < end) {
int mid = start + (end-start) /2;
if(A[mid]>=target) {
end = mid;
} else {
start = mid;
}
}
if(target <= A[start])
return start;
if(target <= A[end]) {
return end;
}
return end+1;
}