forked from lizeyang18/byteDanceAlgorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest37.java
More file actions
71 lines (67 loc) · 2.17 KB
/
test37.java
File metadata and controls
71 lines (67 loc) · 2.17 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package byteDance;
/**
* Created by lizeyang on 2020/5/15.
* 给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。
* 你的算法时间复杂度必须是 O(log n) 级别。
* 如果数组中不存在目标值,返回 [-1, -1]。
*/
public class test37 {
//题目要求了O(logn),显然使用二分查找
public static int[] test(int[] nums, int target) {
int[] res = new int[2];
if (nums == null || nums.length == 0) {
res[0] = -1;
res[1] = -1;
return res;
}
int left = getLeftNum(nums, 0, nums.length - 1, target);
int right = getRightNum(nums, 0, nums.length - 1, target);
if (left > -1 && right > -1) {
res[0] = left;
res[1] = right;
return res;
}
res[0] = -1;
res[1] = -1;
return res;
}
public static int getLeftNum(int[] nums, int l, int r, int target) {
while (l <= r) {
int mid = (l + r) >> 1;
if (nums[mid] == target) {
if (mid > 0 && nums[mid - 1] != target || mid == 0) {
return mid;
} else {
r = mid - 1;
}
} else if (nums[mid] < target) {
l = mid + 1;
} else {
r = mid - 1;
}
}
return -1;
}
public static int getRightNum(int[] nums, int l, int r, int target) {
while (l <= r) {
int mid = (l + r) >> 1;
if (nums[mid] == target) {
if (mid < nums.length - 1 && nums[mid + 1] != target || mid == nums.length - 1) {
return mid;
} else {
l = mid + 1;
}
} else if (nums[mid] < target) {
l = mid + 1;
} else {
r = mid - 1;
}
}
return -1;
}
public static void main(String[] args) {
int[] nums = {5, 7, 7, 8, 8, 10};
int[] res = test(nums, 7);
System.out.println(res[0] + " " + res[1]);
}
}