# LeetCode Search for a Range搜索特定数值的范围 三种方法求解

Given a sorted array of integers, find the starting and ending position of a given target value.

Your algorithm's runtime complexity must be in the order of O(log n).

If the target is not found in the array, return [-1, -1].

For example,
Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].

1 调用STL，不过注意处理调用后的返回值。

vector<int> searchRange(int A[], int n, int target)
{
auto itup = upper_bound(A, A+n, target);
auto itlow = lower_bound(A, A+n, target);
vector<int> res;
if (*itlow == target)
res.push_back(itlow-A);
else res.push_back(-1);
if (*(itup-1) == target)
res.push_back(itup-A-1);
else res.push_back(-1);
return res;
}

2 模仿STL写这个程序，注意处理结果

vector<int> searchRange2(int A[], int n, int target)
{
int step = 0;
int it = 0;
int first = 0;
vector<int> res;
for (int count = n; count>0;)
{
it = first;
step=count/2;
it += step;
if (A[it]<target)
{
first=++it;
count-=step+1;
}
else count=step;
}
if (A[first] != target)
{
res.resize(2,-1);
return res;
}
res.push_back(first);
step = 0;
it = 0;
first = 0;
for (int count = n; count>0;)
{
it = first;
step=count/2;
it += step;
if (!(A[it]>target))
{
first=++it;
count-=step+1;
}
else count=step;
}
res.push_back(first-1);
return res;
}

3 思路：

	vector<int> searchRange3(int A[], int n, int target)
{
int mid = biSearch(A, 0, n-1, target);
vector<int> res(2,-1);
if (mid == -1) return res;
int t = mid;
for (t = mid; t > 0 && A[t] == A[t-1]; t--);
res[0] = t;
for (t = mid; t < n-1 && A[t] == A[t+1]; t++);
res[1] = t;
return res;
}

int biSearch(int A[], int low, int up, int tar)
{
if (low>up) return -1;
int mid = low + ((up-low)>>1);
if (A[mid] < tar)
return biSearch(A, mid+1, up, tar);
if (A[mid] > tar)
return biSearch(A, low, mid-1, tar);
return mid;
}

2014-1-1元旦更新：

vector<int> searchRange4(int A[], int n, int target)
{
vector<int> v(2,-1);
if (n<1) return v;

int low = 0, up = n-1, mid = 0;
while (low <= up)
{
mid = low + ((up-low)>>1);
if (A[mid] >= target) up = mid-1;
else low = mid+1;
}
if (low < n) v[0] = A[low] == target? low:-1;
if (v[0] == -1) return v;

low = 0, up = n-1;
while (low <= up)
{
mid = low + ((up-low)>>1);
if (A[mid] <= target) low = mid+1;
else up = mid-1;
}
v[1] = up;
return v;
}

//2014-1-26 update
class Solution126 {
public:
vector<int> searchRange(int A[], int n, int target)
{
vector<int> rs(2, -1);
int low = 0, up = n-1;
while (low <= up)
{
int mid = low + ((up-low)>>1);
if (A[mid] >= target) up = mid-1;
else low = mid+1;
}
if (low >= n || A[low] != target) return rs;
rs[0] = low;

for (low = 0, up = n-1 ; low <= up; )
{
int mid = low + ((up-low)>>1);
if (A[mid] <= target) low = mid+1;
else up = mid-1;
}
rs[1] = up;
return rs;
}
};

#### LeetCode34搜索范围

2018-03-17 11:12:07

#### leetcode上题目选做：搜索范围

2018-05-01 18:48:57

#### Leetcode 34. 搜索范围

2018-04-24 09:47:22

#### LeetCode 中级 - 搜索范围

2018-05-24 12:25:34

#### leetcode刷题系列C++-candy

2016-02-22 16:23:27

#### [LeetCode]34 范围搜索

2016-02-28 18:40:57

#### LeetCode 34 Search for a Range（搜索范围）

2015-11-28 21:08:06

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

2015-07-27 07:17:03

#### LeetCode算法搜索

2017-06-18 00:13:03

#### leetcode 34. 搜索范围

2018-05-16 21:30:17