-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchInsert.java
More file actions
52 lines (48 loc) · 1.35 KB
/
SearchInsert.java
File metadata and controls
52 lines (48 loc) · 1.35 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package array;
/**
* [35] 搜索插入位置
* @author: Aghao
*
* Given a sorted array of distinct integers 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.
*
* Example 1:
*
* Input: nums = [1,3,5,6], target = 5
* Output: 2
*
* (二分法)
* 时间复杂度:O(log n)
* 空间复杂度:O(1)
*/
public class SearchInsert {
public static void main(String[] args) {
int[] nums = {1,3,5,6};
int target = 0;
// int result = new SearchInsert().searchInsert(nums, target);
int result = new SearchInsert().searchInsertBinary(nums, target);
System.out.println(result);
}
// 1. 暴力法 直接顺序查找对比
private int searchInsert(int[] nums, int target) {
for(int i=0;i<nums.length;i++) {
if(nums[i] >= target) {
return i;
}
}
return nums.length;
}
// 2. 二分查找法 加快查找速度
private int searchInsertBinary(int[] nums, int target) {
int left = 0, right = nums.length;
while(left < right) {
int mid = left + (right - left) / 2;
if(nums[mid] >= target) {
right = mid;
}else {
left = mid + 1;
}
}
return left;
}
}