-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_number_of_k.cpp
84 lines (77 loc) · 1.88 KB
/
get_number_of_k.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <vector>
using namespace std;
/**
* 题目:统计一个数字在升序数组中出现的次数。
* 输入:[1,2,3,3,3,3,4,5],3 输出 4
* 考点:数组,二分
* 分析:
*/
/**
* 解法一:暴力法,遍历
* 时间复杂度:O(n)
*/
class Solution {
public:
int GetNumberOfK(vector<int> data, int k) {
int ret = 0;
for (int d : data) {
if (d == k) {
ret++;
}
}
return ret;
}
};
/**
* 解法二:但是,这并没有用到题干的升序信息,所以还得优化。
* 因为是升序有序数组,所以我们查找一个数可以使用二分法。
* 时间复杂度:O(logn + k)
*/
class Solution2 {
public:
int GetNumberOfK(vector<int> data, int k) {
if (data.size() == 0) {
return 0;
}
int right = data.size() - 1;
int left = 0;
int mid = (right + left) / 2;
;
while (data[mid] != k) {
if (data[mid] > k) {
right = mid;
} else {
left = mid + 1;
}
if (left >= right) {
return 0;
}
mid = (right + left) / 2;
// printf("🍉 min =%d, left = %d, right = %d, data[mid] = %d \n",
// mid,
// left, right, data[mid]);
}
int ret = 0;
for (int i = mid; i < data.size(); i++) {
if (data[i] == k) {
ret++;
} else {
break;
}
}
for (int i = mid - 1; i >= 0; i--) {
if (data[i] == k) {
ret++;
} else {
break;
}
}
return ret;
}
};
int main() {
vector<int> v = {1, 2, 3, 3, 3, 3, 5, 6};
cout << Solution2().GetNumberOfK(v, 3) << endl;
return 0;
}