-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbinary_search.cc
More file actions
37 lines (32 loc) · 795 Bytes
/
Copy pathbinary_search.cc
File metadata and controls
37 lines (32 loc) · 795 Bytes
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
#include "common/common.h"
struct comp {
bool operator()(const int& a, const int& b) const {
return a > b;
}
};
// given an array, starting with all 1's and ending with all 0's.
// find the index of the first appearance of 0
int first_zero(const vector<int>& nums) {
int b = 0;
int e = nums.size();
while (b < e) {
int mid = b + (e - b) / 2;
int tmp = nums[mid];
cout<<"mid element:"<<tmp<<endl;
if (tmp == 1) {
b = mid + 1;
} else {
e = mid;
}
}
return b;
}
void test() {
vector<int> nums {0, 0, 0, };
cout<<first_zero(nums)<<endl;
comp cmp;
cout<<distance(nums.begin(), upper_bound(nums.begin(), nums.end(), 1, comp()))<<endl;
}
int main() {
test();
}