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

原创 2013年12月03日 08:22:07

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,-1]。注意时间效率是O(logN)。这就肯定要用到二分法的思想了。

主要难度是处理好下标的走势。

有三种方法可以求解:

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 思路:

一) 二分法查找到target

二)两边扩张找相等的值

	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;
	}
};


 

版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者允许不得转载。

Excel-VBA的Range.Find问题

前两天一直使用的一个VBA宏突然运行结果错误,仔细核查发现是Range.Find找不到结果: Set searchRng = Sheet3.Range(Sheet3.Cells(2, "C...
  • gtclulong
  • gtclulong
  • 2016年12月07日 08:06
  • 1170

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

题目: 题目翻译: 分析: C++实现: Java实现: Python实现: 感谢阅读,欢迎评论!...
  • dragon_dream
  • dragon_dream
  • 2014年04月03日 20:53
  • 3378

Search for a Range -- LeetCode

原题链接: http://oj.leetcode.com/problems/search-for-a-range/  这道题是二分查找Search Insert Position的变体,思路并不复杂...
  • linhuanmars
  • linhuanmars
  • 2014年03月06日 03:48
  • 16933

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

Problem: Given a sorted array of integers, find the starting and ending position of a given tar...
  • runningtortoises
  • runningtortoises
  • 2015年05月14日 21:28
  • 1170

[LeetCode34]Search for a Range

Given a sorted array of integers, find the starting and ending position of a given target value. Yo...
  • sbitswc
  • sbitswc
  • 2014年06月16日 01:31
  • 5699

LeetCode解题报告--Search for a Range

题目: Given a sorted array of integers, find the starting and ending position of a given target valu...
  • github_27609763
  • github_27609763
  • 2015年12月04日 13:32
  • 1567

leetcode 34 -- Search for a Range

Search for a Range 题目: Given a sorted array of integers, find the starting and ending position...
  • wwh578867817
  • wwh578867817
  • 2015年06月23日 00:36
  • 1195

(Java)LeetCode-34. Search for a Range

Given a sorted array of integers, find the starting and ending position of a given target value. ...
  • u012848330
  • u012848330
  • 2016年08月05日 22:01
  • 111

leetcode_c++:Search for a Range(034)

题目Given a sorted array of integers, find the starting and ending position of a given target value.Yo...
  • mijian1207mijian
  • mijian1207mijian
  • 2016年05月18日 00:45
  • 212

LeetCode 34. Search for a Range(搜索范围)

原题网址:https://leetcode.com/problems/search-for-a-range/ Given a sorted array of integers, find the ...
  • jmspan
  • jmspan
  • 2016年05月20日 10:36
  • 210
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode Search for a Range搜索特定数值的范围 三种方法求解
举报原因:
原因补充:

(最多只允许输入30个字)