LeetCode-35. Search Insert Position

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You may assume no duplicates in the array.

Here are few examples.

1
2
3
4
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0

思路

二分查找,还需要考虑到边界的问题。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public static void main(String[] args) throws Exception {
int[] arr = {1, 3, 5, 6};
int result = new Test().searchInsert(arr, 6);
System.out.println(result);
}

public static int searchInsert(int[] arr, int target) {
if (arr == null || arr.length == 0) {
return -1;
}
int start = 0;
int end = arr.length - 1;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (target == arr[mid]) {
return mid;
} else if (arr[mid] > target) {
end = mid;
} else if (arr[mid] < target) {
start = mid;
}
}
if (arr[start] == target) {
return start;
}
if (arr[end] == target) {
return end;
}
return -1;
}

-------------本文结束感谢您的阅读-------------
Thank you for your encouragement