- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem35.java
More file actions
Latest commit
22 lines (20 loc) · 591 Bytes
/
Copy pathProblem35.java
File metadata and controls
22 lines (20 loc) · 591 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
classProblem35 {
publicstaticvoidmain(String[] args) {
int[] arr = {1,7};
System.out.println(newProblem35().searchInsert(arr, 8));
}
publicintsearchInsert(int[] nums, inttarget) {
if (target <= nums[0]) {
return0;
}
if (target == nums[nums.length-1]) {
returnnums.length-1;
}
for (inti = 0; i < nums.length-1; i++) {
if (nums[i] <=target && nums[i+1] > target) {
returnnums[i] != target ? i+1 : i;
}
}
returnnums.length;
}
}