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].
给定一个有序数组和一个目标值,查找出目标值在数组中出现的发生下标区域。时间复杂度要求log(N)。
解法
刚开始以为下标也需要用二分来查找,但看讨论发现大家都是找出目标值然后再向两边搜索-_-b,这样做的话,最差的情况下(数组里的数全都一样)就是O(N)了,但是也能过。
class Solution
{
public:
vector<int> searchRange(vector<int>& nums, int target)
{
int left = 0;
int right = nums.size() - 1;
int index_a = -1;
int index_b = -1;
int index = 0;
bool flag = false;
while(left <= right)
{
int mid = (left + right) >> 1;
if(nums[mid] == target)
{
flag = true;
index = mid;
break;
}
else
if(nums[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
if(flag)
index_a = index;
while(index_a >= 1 && nums[index_a - 1] == target)
index_a --;
if(flag)
index_b = index;
while(index_b < nums.size() - 1 && nums[index_b + 1] == target)
index_b ++;
vector<int> rt = {index_a,index_b};
return rt;
}
};
LeetCode Search for a Range (二分查找)的更多相关文章
- LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- LeetCode Search Insert Position (二分查找)
题意: 给一个升序的数组,如果target在里面存在了,返回其下标,若不存在,返回其插入后的下标. 思路: 来一个简单的二分查找就行了,注意边界. class Solution { public: i ...
- Leetcode之二分法专题-704. 二分查找(Binary Search)
Leetcode之二分法专题-704. 二分查找(Binary Search) 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 t ...
- [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 解题报告
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 278 First Bad Version 二分查找(二分下标)
题意:找到第一个出问题的版本 二分查找,注意 mid = l + (r - l + 1) / 2;因为整数会溢出 // Forward declaration of isBadVersion API. ...
- LeetCode First Bad Version (二分查找)
题意: 有一个bool序列表示对应下标的版本是否出问题(下标从1开始),如果一个版本出了问题,那么其后面全部版本必定出问题.现在给出判断任意版本是否出问题的API,请找到第一个出问题的版本. 思路: ...
- [LeetCode] Search for a Range [34]
题目 Given a sorted array of integers, find the starting and ending position of a given target value. ...
随机推荐
- [Linux|DBA]运维三十六计
这里是腾讯两位大神梁定安.周小军总记得运维DBA三十六计--
- python 多进程和子进程1
多进程的缓冲区 #多进程 process.py from multiprocessing import Process,current_process import time def func1(): ...
- Linux磁盘空间占满问题快速定位
1.df -h命令查看系统盘与各个磁盘的占用空间比率 [tidb@:vg_adn_tidbCkhsTest:172.31.30.62 /dev]$df -Th Filesystem Type Size ...
- 自定义上传控件(兼容IE8)
上传控件是 <input type="file"/> 而实际开发过程中,都会自定义一个控件,因为这个控件本身难看,而且不同浏览器效果不一样. 如IE8显示如下: 谷歌浏 ...
- MyBatis 中的级联
MyBatis 的级联分为 3 种. 1.鉴别器(discriminator):它是根据某些条件决定采用具体实现类级联的方案,比如体检表要根据性别去区分. 2.一对一(association):比如学 ...
- node+koa中转层开发实践总结
node中转层的意义: 1.能解决前后端代码部署在不同服务器下时的跨域问题.(实现) 2.合并请求,业务逻辑处理.(实现) 3.单页应用的首屏服务端渲染.(暂未实现) 环境准备: node: ^8.1 ...
- 如何安装Firebug
Firebug是firefox下的一个插件,能够调试所有网站语言,如Html,Css等,但FireBug最吸引我的就是javascript调试功能,使用起来非常方便,而且在各种浏览器下都能使用(IE, ...
- 2298: [HAOI2011]problem a
Description 一次考试共有n个人参加,第i个人说:"有ai个人分数比我高,bi个人分数比我低."问最少有几个人没有说真话(可能有相同的分数) 这句话的意思就等于是说第i个 ...
- 图解:图形下控制台中weblogic9.2多池配置为oracle集群RAC
update: 这个东西如果配置不顺利的话:应用请求数据库的时候,会打印类似这样的错误: :open connection err Pool connect failed : weblogic.com ...
- 通过命令行Pandoc 来转换文件
Pandoc 是一个命令行工具,用于将文件从一种标记语言转换为另一种标记语言.标记语言使用标签来标记文档的各个部分.常用的标记语言包括 Markdown.ReStructuredText.HTML.L ...