-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·56 lines (48 loc) · 1.38 KB
/
main.cpp
File metadata and controls
executable file
·56 lines (48 loc) · 1.38 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
#include <iostream>
#include <vector>
#include <iostream>
#include <vector>
#include <string>
int search(const std::vector<std::string>& strings, const std::string& str) {
int low = 0;
int high = strings.size() - 1;
while (high >= low) {
int mid = high - (high - low) / 2;
if (strings[mid].empty()) {
int left = mid - 1;
int right = mid + 1;
while (left >= low || right <= high) {
if (left >= low) {
if (!strings[left].empty()) {
mid = left;
break;
}
}
if (right <= high) {
if (!strings[right].empty()) {
mid = right;
break;
}
}
left--;
right++;
}
}
if (strings[mid].empty()) {
return -1;
}
if (strings[mid] == str) {
return mid;
} else if (strings[mid] > str) {
high = mid - 1;
} else if (strings[mid] < str) {
low = mid + 1;
}
}
return -1;
}
int main() {
std::vector<std::string> strings {"abe", "", "", "", "", "joe", "", "zz"};
std::cout << search(strings, "zz") << std::endl;
return 0;
}