-
Notifications
You must be signed in to change notification settings - Fork 19
/
searchInsertPosition.java
55 lines (51 loc) · 1.47 KB
/
searchInsertPosition.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* Created by codingBoy on 17/2/6.
*/
public class searchInsertPosition
{
/**
* 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.
* @param args
* 给定排序的数组和目标值,如果找到目标,则返回索引。如果没有,返回它被插入顺序数组中的索引。
*/
public static void main(String[] args)
{
System.out.println(searchInsert(new int[]{1,3,5,6,8,10,12,16,18,20},9));
}
public static int searchInsert(int[] nums, int target) {
if (target<nums[0])
{
return 0;
}
if (target>nums[nums.length-1])
{
return nums.length;
}
for (int i=0;i<nums.length;i++)
{
if (nums[i]==target)
{
return i;
}
if (target>nums[i]&&target<nums[i+1])
{
return i+1;
}
}
return 0;
}
/**
* 另外下面这个是我在网上看到的一个效率更高的使用二分法解决 的
* public int searchInsert(int[] A, int target) {
int low = 0, high = A.length-1;
while(low<=high){
int mid = (low+high)/2;
if(A[mid] == target) return mid;
else if(A[mid] > target) high = mid-1;
else low = mid+1;
}
return low;
}
*/
}