[1,3,5,6], 0 → 0
分析:
二分查找,不同点就是开始时判断target与两边界点的关系
public class Solution { public int searchInsert(int[] A, int target) { //二分查找 return searchInsert(A, target, 0, A.length-1); } public int searchInsert(int[] A, int target, int start, int end){ if(target < A[start]) return start; if(target > A[end]) return end+1; int mid = (start+end)/2; if(target == A[mid]) return mid; if(target < A[mid]) return searchInsert(A, target, start, mid-1); else return searchInsert(A, target, mid+1, end); } }