-
Notifications
You must be signed in to change notification settings - Fork 0
/
5.cpp
31 lines (29 loc) · 1001 Bytes
/
5.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
/*************************************************************************
> File Name: 5.cpp
> Author: Louis1992
> Mail: [email protected]
> Blog: http://gzc.github.io
> Created Time: Mon Nov 2 17:39:52 2015
************************************************************************/
#include<iostream>
#include<vector>
using namespace std;
int findGivenStr(vector<string> &myvec, string target) {
int i(0), j(myvec.size()-1);
while(i < j) {
int mid = (i+j) >> 1;
while(mid < myvec.size() && myvec[mid] == " ") mid++;
if(mid > j) { j = (i+j)/2 - 1; continue;}
if(myvec[mid] == target) return mid;
if(myvec[mid] > target) j = mid - 1;
else i = mid + 1;
}
return myvec[i] == target? i : -1;
}
int main() {
vector<string> testcase1 {" ", " ", " "};
vector<string> testcase2 {" ", " ", "hello"};
cout << findGivenStr(testcase1, "hello") << endl;;
cout << findGivenStr(testcase2, "hello") << endl;;
return 0;
}