代码:
1 #include<iostream> 2 3 using namespace std; 4 5 int searchInsert(int A[], int n, int target) { 6 int l = 0; 7 int r = n - 1; 8 while (l <= r){ 9 int mid = (l + r) / 2; 10 if (target < A[mid]){ 11 r = mid - 1; 12 } 13 else if (target > A[mid]){ 14 l = mid + 1; 15 } 16 else{ 17 return mid; 18 } 19 } 20 21 return r + 1; 22 } 23 24 int main(){ 25 int A[] = {1}; 26 cout << searchInsert(A, 1, 2) << endl; 27 }