-
Notifications
You must be signed in to change notification settings - Fork 7
/
number_k.go
52 lines (48 loc) · 957 Bytes
/
number_k.go
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
package main
/**
*
* @param data int整型一维数组
* @param k int整型
* @return int整型
*/
// https://www.nowcoder.com/practice/70610bf967994b22bb1c26f9ae901fa2?tpId=13&tqId=11190&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
func GetNumberOfK(data []int, k int) int {
low, high := 0, len(data)-1
findIdx := -1
for low <= high {
mid := (low + high) / 2
if data[mid] == k {
findIdx = mid
break
}
if data[mid] > k {
high = mid - 1
} else {
low = mid + 1
}
}
if findIdx == -1 {
return 0
}
lowCnt := 0
for high := findIdx - 1; low <= high; {
mid := (low + high) / 2
if data[mid] == k {
lowCnt = findIdx - mid
high = mid - 1
} else {
low = mid + 1
}
}
highCnt := 0
for low := findIdx + 1; low <= high; {
mid := (low + high) / 2
if data[mid] == k {
highCnt = mid - findIdx
low = mid + 1
} else {
high = mid - 1
}
}
return lowCnt + highCnt + 1
}