-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path34.cpp
More file actions
executable file
·60 lines (59 loc) · 1.56 KB
/
34.cpp
File metadata and controls
executable file
·60 lines (59 loc) · 1.56 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
56
57
58
59
60
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> answer;
if (nums.size() == 0){
answer.push_back(-1);
answer.push_back(-1);
return answer;
}
vector<int>::iterator first = lower_bound(nums.begin(), nums.end(),target);
vector<int>::iterator end = upper_bound(nums.begin(), nums.end(),target);
//cout<<*first<<endl;
int index1,index2;
if (first == nums.end())
index1 = -1;
else{
if (*first == target)
index1 = first - nums.begin();
else
index1 = -1;
}
answer.push_back(index1);
cout<<*end<<endl;
if (end == nums.end()){
index2 = -1;
answer.push_back(-1);
}
else {
if (*first == target){
index2 = end - nums.begin();
if (*end == target)
answer.push_back(index2);
else
answer.push_back(index2-1);
}
else
{
index2 = -1;
answer.push_back(index2);
}
}
return answer;
}
};
int main(){
Solution s;
vector<int> tmp;
tmp.push_back(3);
//tmp.push_back(2);
/*tmp.push_back(3);
tmp.push_back(3);
tmp.push_back(4);
tmp.push_back(6);*/
cout<<s.searchRange(tmp,3)[0]<<" "<<s.searchRange(tmp,3)[1]<<endl;
}