public int searchInsert(int[] A, int target) {
if (A == null || A.length == 0) {
return -1;
}
int start = -1, end = A.length;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (A[mid] == target) {
return mid; // no duplicates
} else if (A[mid] < target) {
start = mid;
} else {
end = mid;
}
}
return start + 1;
}
}
Search Insert position( 二分查找)
最新推荐文章于 2021-01-14 03:47:03 发布