-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathSolution.java
More file actions
33 lines (33 loc) · 843 Bytes
/
Solution.java
File metadata and controls
33 lines (33 loc) · 843 Bytes
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
public class Solution {
public int searchInsert(int[] nums, int target) {
int begin=0;
int end=nums.length-1;
int a=(begin+end)/2;
if (target<nums[0]) {
return 0;
}
if (target>nums[nums.length-1]) {
return nums.length;
}
while (nums[a]!=target) {
if (end-1==begin&&(nums[begin]<target&&target<nums[end])) {
return end;
}
if (nums[a]>target) {
if (end==a) {
end--;
}else {
end=a;
}
}else {
if (begin==a) {
begin++;
}else {
begin=a;
}
}
a=(begin+end)/2;
}
return a;
}
}