-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSolution35.java
More file actions
executable file
·41 lines (32 loc) · 868 Bytes
/
Solution35.java
File metadata and controls
executable file
·41 lines (32 loc) · 868 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
34
35
36
37
38
39
40
41
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Created by slade on 2019/6/3.
*/
public class Solution35 {
public int searchInsert(int[] nums, int target) {
int idx=0;
if (nums[nums.length - 1] < target) {
return nums.length;
}
if (nums[0] > target) {
return 0;
}
for (int i = 0; i < nums.length; i++) {
System.out.println(nums[i]);
if (nums[i] == target) {
idx = i;
} else if (target > nums[i] && target < nums[i + 1]) {
idx = i+1;
}
}
return idx;
}
public static void main(String[] args) {
Solution35 s = new Solution35();
int[] a = {1, 2, 4, 5};
int b = 3;
System.out.println(s.searchInsert(a, b));
}
}