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 (binary search)
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=324616551&siteId=291194637
Recommended
Ranking