[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. ...
随机推荐
- LightOJ1044 Palindrome Partitioning(区间DP+线性DP)
问题问的是最少可以把一个字符串分成几段,使每段都是回文串. 一开始想直接区间DP,dp[i][j]表示子串[i,j]的答案,不过字符串长度1000,100W个状态,一个状态从多个状态转移来的,转移的时 ...
- BZOJ4289 : PA2012 Tax
一个直观的想法是把每条边拆成两条有向边,同时每条有向边是新图中的一个点.对于两条边a->b与b->c,两点之间连有向边,费用为两条边费用的最大值.然后新建源点S与汇点T,由S向所有起点为1 ...
- 【BZOJ】1070: [SCOI2007]修车(费用流+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=1070 好神的题!!!orz 首先我是sb不会拆点..... 首先,每一个技术人员维修车辆都有一个先后 ...
- NOI2011阿狸的打字机(fail树+DFS序)
Description 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母. 经阿狸研究发现,这个打字机是这样工作的 ...
- oracle函数listagg的使用说明(分组后连接字段)
关于oracle函数listagg的使用说明 工作中经常遇到客户提出这样的需求,希望在汇总合并中,能够把日期逐个枚举出来. 如图,原始数据是这样的: 客户希望能够实现这样的汇总合并: 那么通常我会使用 ...
- 李洪强-C语言7-C语言运算符
C语言运算符 一.算术运算 C语言一共有34种运算符,包括常见的加减乘除运算. ①. 加法:+ 还可以表示正号 ②. 减法:- 还可以表示负号 ③. 乘法:* 非数学意义上的X ④. 除法:/ 注意 ...
- sftp配置
sftp不需要安装,只需要借助sshd服务器即可使用. 增加用户useradd -s /bin/false mysftp 设置用户密码passwd mysftp 创建用户家目录mkdir /home/ ...
- ajax无刷新获取php后台数据
$.ajax({ url:"result.php", //data:{"page":i}, dataType:"json", beforeS ...
- uva729
/*题目一大堆,其实意思就是长度为n个二进制数,里面有h个1,将这个二进制数进行全排列,然后输出*/ #include"iostream" #include"algori ...
- 开源top100
1.SwitchyOmega 项目简介:SwitchyOmega 是 SwitchySharp 的新版本.这是一个 Chrome 浏览器用来切换不同代理的插件.SwitchyOmega 初次安装时会检 ...