-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path35.java
36 lines (32 loc) · 827 Bytes
/
35.java
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
class Solution {
public int searchInsert(int[] nums, int target) {
int left = -1;
int right = nums.length;
while (left + 1 < right) {
int mid = left + (right - left) / 2;
if (nums[mid] >= target) {
right = mid;
} else {
left = mid;
}
}
return right;
}
}
// 第二次做法,彻底搞懂红蓝染色和开闭区间
class Solution {
public int searchInsert(int[] nums, int target) {
int left = 0;
int right = nums.length-1;
while(left<=right){
int mid = left + (right-left)/2;
if(nums[mid]<target){
left = mid+1;
}
else{
right = mid-1;
}
}
return left;
}
}