-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchForARange.java
More file actions
49 lines (47 loc) · 1.3 KB
/
SearchForARange.java
File metadata and controls
49 lines (47 loc) · 1.3 KB
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
39
40
41
42
43
44
45
46
47
48
49
package com.leetcode;
/**
* Created by jamylu on 2018/1/18.
* leetcode034.
* 给定一个有序数组,找出给定值所在的下标范围,要求是二分搜索
*/
public class SearchForARange {
public static void main(String[] args) {
int[] arr = {1};
int target = 1;
int[] ans = searchRange(arr, target);
System.out.println(ans[0] + " " + ans[1]);
}
public static int[] searchRange(int[] nums, int target) {
int left = 0;
int right = nums.length;
int mid;
int ansLeft;
int ansRight;
// 找左边界
while (left < right) {
mid = (left + right) / 2;
if (nums[mid] >= target) {
right = mid;
} else {
left = mid + 1;
}
}
ansLeft = left;
if (nums == null || ansLeft == nums.length || nums[ansLeft] != target) {
return new int[]{-1, -1};
}
left = 0;
right = nums.length;
// 找右边界
while (left < right) {
mid = (left + right) / 2;
if (nums[mid] > target) {
right = mid;
} else {
left = mid + 1;
}
}
ansRight = left - 1;
return new int[]{ansLeft, ansRight};
}
}