解答
C++
/**
* Search Insert Position
* 搜索插入位置
*/
#include <stdio.h>
int binary_search(int A[], int n, int key)
{
int low = 0;
int high = n-1;
while (low <= high)
{
int mid = low +(high-low)/2;
if (A[mid] == key)
{
return mid;
}
if ( key> A[mid] )
{
low = mid+1;
}
else
{
high = mid-1;
}
}
return low;
}
int searchInsert(int A[], int n, int target)
{
if (n==0) return n;
return binary_search(A, n, target);
}
int main()
{
int a[]={1,3,5,6};
printf("%d -> %d\n", 5, searchInsert(a, 4, 5));
printf("%d -> %d\n", 2, searchInsert(a, 4, 2));
printf("%d -> %d\n", 7, searchInsert(a, 4, 7));
printf("%d -> %d\n", 0, searchInsert(a, 4, 0));
return 0;
}
python
/**
* Search Insert Position
* 搜索插入位置
*/
class Solution(object):
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
lo = 0
hi = len(nums)
while lo < hi:
mid = lo + (hi - lo) / 2
if nums[mid] > target:
hi = mid
elif nums[mid] < target:
lo = mid + 1
else:
return mid
return lo