-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch-insert_pos.java
More file actions
32 lines (30 loc) · 865 Bytes
/
search-insert_pos.java
File metadata and controls
32 lines (30 loc) · 865 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
public class Solution {
public int searchInsert(int[] A, int target) {
// Start typing your Java solution below
// DO NOT write main() function
int l=0,r=A.length-1;
while(l<=r) {
int mid=l+(r-l)/2;
if(A[mid]>target) r=mid-1;
else if(A[mid]<target) l=mid+1;
else return mid;
}
//if(target>A[l]) return l+1;
return l;
}
}
public class Solution {
public int searchInsert(int[] A, int target) {
// Start typing your Java solution below
// DO NOT write main() function
int l=0,r=A.length-1;
while(l<r) {
int mid=l+(r-l)/2;
if(A[mid]>target) r=mid-1;
else if(A[mid]<target) l=mid+1;
else return mid;
}
if(target>A[l]) return l+1;
return l;
}
}