# 34. Search for a Range

class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
int n=nums.size();
int left=0,right=n-1,mid;
int L,R;
vector<int> ans{-1,-1};
bool find=false;
while(left<=right)
{
mid=(left+right)>>1;
if(nums[mid]==target)
{
find=true;
break;
}
else if(nums[mid]>target)
right=mid-1;
else
left=mid+1;
}
if(!find)
return ans;

left=0,right=mid;
while(left<=right)
{
mid=(left+right)>>1;
if(nums[mid]==target)
right=mid-1;
else if(nums[mid]<target)
left=mid+1;
}
L=left;
left=mid,right=n-1;
while(left<=right)
{
mid=(left+right)>>1;
if(nums[mid]==target)
left=mid+1;
else if(nums[mid]>target)
right=mid-1;
}
R=right;
ans[0]=L;
ans[1]=R;
return ans;
}
};

#### LeetCode 34 — Search for a Range（C++ Java Python）

2014-04-03 20:53:45

#### LeetCode 34 Search for a Range (C,C++,Java,Python)

2015-05-14 21:28:11

#### leetcode 34 -- Search for a Range

2015-06-23 00:36:45

#### Search for a Range -- LeetCode

2014-03-06 03:48:49

#### [LeetCode34]Search for a Range

2014-06-16 01:31:31

#### 【LeetCode-面试算法经典-Java实现】【034-Search for a Range（搜索一个范围）】

2015-07-27 07:17:03

#### leetcode_c++：Search for a Range（034）

2016-05-18 00:45:22

#### 34. Search for a Range

2018-04-20 15:48:02

#### 34. Search for a Range**

2017-01-10 20:27:45

#### LeetCode解题报告--Search for a Range

2015-12-04 13:32:03