-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSearchInsertPosition.java
More file actions
38 lines (33 loc) · 1013 Bytes
/
SearchInsertPosition.java
File metadata and controls
38 lines (33 loc) · 1013 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
package binarysearch;
// Source : https://leetcode.com/problems/search-insert-position/
// Id : 35
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Date : 2020-01-01
// Topic : Binary Search
// Level : Easy
// Other :
// Tips :
// Result : 100.00% 5.03%
public class SearchInsertPosition {
// 100.00% 0ms 5.03%
public int searchInsert(int[] nums, int target) {
if (nums.length == 0)
return 0;
int left = 0, right = nums.length - 1, k;
while (left < right) {
k = (left + right) / 2;
if (nums[k] == target)
return k;
if (nums[k] < target) {
left = k + 1;
} else {
right = k - 1;
}
}
return nums[left] < target ? left + 1 : left;
}
public static void main(String[] args) {
SearchInsertPosition s = new SearchInsertPosition();
System.out.println(s.searchInsert(new int[]{1,3}, 4));
}
}