Skip to content

Commit 8fca8ee

Browse files
committed
solution for: search insert position
1 parent 2ccbf8a commit 8fca8ee

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package leetcode;
2+
3+
public class SearchInsertPosition {
4+
public static void main(String[] args){
5+
SearchInsertPosition test = new SearchInsertPosition();
6+
System.out.println(test.searchInsert(new int[] {1,3,5,6}, 2));
7+
}
8+
9+
public int searchInsert(int[] nums, int target) {
10+
int l=0, h=nums.length-1;
11+
while(l<h){
12+
int mid = (l+h)/2;
13+
if(target> nums[mid]){
14+
l = mid+1;
15+
}else{
16+
h=mid;
17+
}
18+
}
19+
return h;
20+
21+
}
22+
}

0 commit comments

Comments
 (0)