forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
51 lines (49 loc) · 1.15 KB
/
Solution.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
class Solution {
public int searchInsert(int[] nums, int target) {
if(nums.length == 0){
return 0;
}
if(nums.length == 1){
if(nums[0] < target){
return 1;
} else {
return 0;
}
}
for(int i = 0;i < nums.length;i++){
if(nums[i] == target){
return i;
} else {
int s = Math.min(nums[i],target);
if(s == target){
return i;
}
}
}
return nums.length;
}
}
/*
// 二分法
class Solution {
public int searchInsert(int[] nums, int target) {
if (nums == null || nums.length == 0) {
return 0;
}
int low = 0;
int high = nums.length - 1;
while (low <= high) {
int mid = low + ((high - low) >> 1);
if (nums[mid] == target) {
return mid;
}
if (nums[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return low;
}
}
*/