[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].
class Solution {
public:
vector<int> searchRange(int A[], int n, int target) {
set<int> sIndex;
search(A,,n-,target,sIndex);
vector<int> result(,-);
if(!sIndex.empty()){
result[] = *sIndex.begin();
result[] = *(--sIndex.end());
}
return result;
}
private:
void search(int A[],int start,int end,int target,set<int> &sIndex){
if(start == end){
if(A[start]==target){
sIndex.insert(start);
return;
}else
return;
}
int startIndex,endIndex;
int mid = start + (end-start)/;
if(A[start]<=target && A[mid]>= target)
search(A,start,mid,target,sIndex);
if(A[mid+]<=target && A[end]>= target)
search(A,mid+,end,target,sIndex);
}//end func
};
[LeetCode] Search for a Range(二分法)的更多相关文章
- leetcode 34 Search for a Range(二分法)
Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...
- LeetCode: Search for a Range 解题报告
Search for a RangeGiven a sorted array of integers, find the starting and ending position of a given ...
- [LeetCode] Search for a Range 搜索一个范围
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- leetcode Search for a Range python
class Solution(object): def searchRange(self, nums, target): """ :type nums: List[int ...
- [LeetCode] Search for a Range 二分搜索
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- Leetcode Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- leetcode:Search for a Range(数组,二分查找)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- leetcode -- Search for a Range (TODO)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- [LeetCode] Search for a Range [34]
题目 Given a sorted array of integers, find the starting and ending position of a given target value. ...
随机推荐
- BZOJ3838 : [Pa2013]Raper
将选取的$A$看成左括号,$B$看成右括号,那么答案是一个合法的括号序列. 那么只要重复取出$k$对价值最小的左右括号,保证每时每刻都是一个合法的括号序列即可. 将$($看成$1$,$)$看成$-1$ ...
- BZOJ2584 : [Wc2012]memory
xy方向分开考虑 用扫描线处理出拓扑序,第二问直接回答拓扑序, 第一问: 将操作倒过来,变成加入线段,用线段树维护区间拓扑序的最值 #include<cstdio> #include< ...
- 【POJ】1151 Atlantis(线段树)
http://poj.org/problem?id=1151 经典矩形面积并吧.....很简单我就不说了... 有个很神的地方,我脑残没想到: 将线段变成点啊QAQ这样方便计算了啊 还有个很坑的地方, ...
- 【BZOJ】2648: SJY摆棋子 & 2716: [Violet 3]天使玩偶(kdtree)
http://www.lydsy.com/JudgeOnline/problem.php?id=2716 http://www.lydsy.com/JudgeOnline/problem.php?id ...
- 【BZOJ】1066: [SCOI2007]蜥蜴(最大流)
http://www.lydsy.com/JudgeOnline/problem.php?id=1066 本题想一想应该懂了的. 我们想啊,,每个点都有限制,每个点都可以跳到另一个有限制的点,每个有蜥 ...
- 【COGS】714. USACO 1.3.2混合牛奶(贪心+水题)
http://cojs.tk/cogs/problem/problem.php?pid=714 在hzwer的刷题记录上,默默地先跳过2题T_T...求凸包和期望的..T_T那是个啥..得好好学习 看 ...
- 使用Qt 开发图形界面的软件
3DSlicer, a free open source software for visualization and medical image computing AcetoneISO:镜像文件挂 ...
- 在cmd下输入/g无效
如图: 原来一:斜杠得是\ 二:命令和它之间没空格.这个符号和分号的使用是一样的.
- Asp反向代理程序,调用远程站点全站数据,一款脚本级反向代理程序.
前些天临时写的一脚本级反向代理程序,用法很简单,设置好目标站地址,然后放到你网站根目录:index.asp,再将404页面自定义为:index.asp,即可. 由于暂时没有 url 替换需要,所以没有 ...
- gnuplotx轴的logscale显示
假设数据是这样子的: gnuplot脚本如下: set terminal postscript eps color enhanced set log x set log y set format x ...