-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
53 lines (51 loc) · 1.45 KB
/
Copy pathmain.cpp
File metadata and controls
53 lines (51 loc) · 1.45 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
//
// main.cpp
// leetcode034
//
// Created by 萧天牧 on 17/2/28.
// Copyright © 2017年 萧天牧. All rights reserved.
//
#include <iostream>
#include <vector>
using namespace std;
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> res = {-1, -1};
int left = 0, right = nums.size() - 1;
if (nums.size() == 0)
return res;
while(left < right){
int mid = (left + right) >> 1;
if (nums[mid] < target){
left = mid + 1;
}
else if (nums[mid] > target)
right = mid - 1;
else//nums[mid] == target时,证明最左边匹配的在[left,mid]中,将right置为mid.
right = mid;
}
if (nums[left] != target)
return res;
res[0] = left;
left = 0, right = nums.size() - 1;
while(left < right){
int mid = ((left + right) >> 1) + 1;
if (nums[mid] < target)
left = mid + 1;
else if (nums[mid] > target)
right = mid - 1;
else //nums[mid] == target时,证明最右值在[mid,right]中,将left置为mid
left = mid;
//当把mid置为(left + right) / 2 + 1时,整体向右偏。
}
res[1] = right;
return res;
}
void printRes(vector<int> res){
for (int i = 0; i < res.size() ; i++)
cout << res[i] << endl;
}
int main(int argc, const char * argv[]) {
vector<int> a = {1,1,2,3,4,5,5,5,6};
printRes(searchRange(a, 5));
return 0;
}