-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstPositionOfTarget.java
More file actions
31 lines (26 loc) · 953 Bytes
/
FirstPositionOfTarget.java
File metadata and controls
31 lines (26 loc) · 953 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
public class FirstPositionOfTarget {
/**
For a given sorted array (ascending order) and a target number, find the first index of
this number in O(log n) time complexity.
If the target number does not exist in the array, return -1.
Example
If the array is [1, 2, 3, 3, 4, 5, 10], for given target 3, return 2.
Challenge
If the count of numbers is bigger than 2^32, can your code work properly?
*/
public int binarySearch(int[] nums, int target) {
if (nums == null) return -1;
int start = 0, end = nums.length - 1;
while (start < end - 1) {
int mid = start + (end - start) / 2;
if (nums[mid] < target) {
start = mid;
} else {
end = mid;
}
}
if (nums[start] == target) return start;
if (nums[end] == target) return end;
return -1;
}
}