forked from geekxh/hello-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
69 lines (65 loc) · 1.72 KB
/
Solution.java
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
/**
* @author Anonymous
* @since 2019/12/8
*/
class Solution {
/**
* 求数字k在排序数组中出现的次数
*
* @param nums 数组
* @param k 数字k
* @return k在数组中出现的次数
*/
public int getNumberOfK(int[] nums, int k) {
if (nums == null || nums.length == 0) {
return 0;
}
int start = 0, end = nums.length - 1;
int first = getFirstK(nums, start, end, k);
int last = getLastK(nums, start, end, k);
if (first > -1 && last > -1) {
return last - first + 1;
}
return 0;
}
private int getFirstK(int[] nums, int start, int end, int k) {
if (start > end) {
return -1;
}
int m = start + ((end - start) >> 1);
if (nums[m] == k) {
if (m == 0 || (m > 0 && nums[m - 1] != k)) {
return m;
} else {
end = m - 1;
}
} else {
if (nums[m] > k) {
end = m - 1;
} else {
start = m + 1;
}
}
return getFirstK(nums, start, end, k);
}
private int getLastK(int[] nums, int start, int end, int k) {
if (start > end) {
return -1;
}
int m = start + ((end - start) >> 1);
if (nums[m] == k) {
if (m == nums.length - 1 || (m < nums.length - 1 && nums[m + 1] != k)) {
return m;
} else {
start = m + 1;
}
} else {
if (nums[m] > k) {
end = m - 1;
} else {
start = m + 1;
}
}
return getLastK(nums, start, end, k);
}
}