简单题,二分。
找到了返回下标,找不到返回应该放的位置的下标。
class Solution { public: int ans; int searchInsert(int A[], int n, int target) { // Note: The Solution object is instantiated only once and is reused by each test case. ans = 0; bsearch(0,n-1,A,target); return ans; } void bsearch(int left,int right,int A[],int target) { if(left > right) { ans = left; return ; } int mid = (left + right) / 2; if(A[mid] == target) { ans = mid; return ; } else if(A[mid] > target) { bsearch(left,mid-1,A,target); } else { bsearch(mid+1,right,A,target); } } };