[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. ...
随机推荐
- HDU3996 Gold Mine(最大权闭合子图)
#include<cstdio> #include<cstring> #include<queue> #include<algorithm> using ...
- LightOJ1316 A Wedding Party(状压DP)
这题事实上只需要关心15个商店和一个起点一个终点,预处理出这几个点之间的最短距离.Floyd会超时,用Dijkstra即可. 然后就是dp[u][S]表示已经经过商店集合S且当前在第u个商店所花的最少 ...
- ural 1156. Two Rounds
1156. Two Rounds Time limit: 2.0 secondMemory limit: 64 MB There are two rounds in the Urals Champio ...
- 【POJ】2187 Beauty Contest(旋转卡壳)
http://poj.org/problem?id=2187 显然直径在凸包上(黑书上有证明).(然后这题让我发现我之前好几次凸包的排序都错了QAQ只排序了x轴.....没有排序y轴.. 然后本题数据 ...
- 使用GCD
使用GCD 什么是 GCD Grand Central Dispatch (GCD) 是 Apple 开发的一个多核编程的解决方法.该方法在 Mac OS X 10.6 雪豹中首次推出,并随后被引入到 ...
- thinkphp中M()和D()的理解
在tp框架中基于MVC设计模式中的model文件夹下,处理数据时会创建和表相关的模型类文件.在控制器中需要使用时需要实例化模型类对象,写语句 1.$a = new GoodsModel(); 这是基于 ...
- Stackoverflow架构
Stackoverflow用的是.net开发的,用的缓存是Redis,Stackoverflow架构的演讲地址是:http://www.infoq.com/cn/presentations/archi ...
- 【液晶模块系列基础视频】5.3.X-GUI字体驱动3
============================= 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:ht ...
- 2016.05.03,英语,《Vocabulary Builder》Unit 21
sub, means 'under', as in subway, submarine, substandard. A subject is a person who is under the aut ...
- PHP mkdir 方法 创建 0777 权限的目录问题
php 中使用 mkdir() 方法创建 0777 权限的目录: $path = './Logs/secondCheck/';if(!is_dir($path)){ mkdir($path, 0777 ...