leetCode题解之寻找一个数在有序数组中的范围Search for a Range
1、问题描述
Given an array of integers sorted in ascending order, 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].
2、问题分析
题目要求在log(N)的时间内。选择二分查找,现在数组中找出该数的一个位置,然后向左右两边扫描,找出左边的位置和右边的位置。
3、代码
vector<int> searchRange(vector<int>& nums, int target) {
// 数组是有序的 ,先用二分查找找出一个目标数。然后向两边寻找
vector<int> ans;
if(nums.size() == )
{
ans.push_back(-);
ans.push_back(-);
return ans;
}
int left = ,right = nums.size()-;
int index = -;
while(left <= right)
{
int mid = left + (right - left)/;
if(nums[mid] == target)
{
index = mid;
break;
}
else if( nums[mid] > target )
right = mid -;
else
left = mid + ;
}
// target doesn't exist in array
if( index == -)
{
ans.push_back(-);
ans.push_back(-);
return ans;
}
// target exist in array
int indexL = index;
int indexR = index;
while( nums[indexL] == target && indexL >= ) indexL--;
while( nums[indexR] == target && indexR < nums.size() ) indexR++;
int n = nums.size();
ans.push_back(indexL+);
ans.push_back(indexR-);
return ans;
leetCode题解之寻找一个数在有序数组中的范围Search for a Range的更多相关文章
- [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Single Element in a Sorted Array 有序数组中的单独元素
Given a sorted array consisting of only integers where every element appears twice except for one el ...
- leetCode题解之寻找插入位置
1.问题描述 Search Insert Position Given a sorted array and a target value, return the index if the targe ...
- LeetCode 26 Remove Duplicates from Sorted Array (移除有序数组中重复数字)
题目链接: https://leetcode.com/problems/remove-duplicates-from-sorted-array/?tab=Description 从有序数组中移除重 ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)
[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)
https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...
随机推荐
- 爬虫实战(二) 51job移动端数据采集
在上一篇51job职位信息的爬取中,对岗位信息div下各式各样杂乱的标签,简单的Xpath效果不佳,加上string()函数后,也不尽如人意.因此这次我们跳过桌面web端,选择移动端进行爬取. ...
- spark、hadoop集群添加节点
1.首先添加hdfs的节点,将安装包上传到服务器,设置好环境变量.配置文件按之前spark集群搭建的那里进行修改. 设置完成后,要对新节点新型格式化: # hdfs dfs namenode - ...
- docker-compose.yml样例(mysql主从+mycat读写分离)
Docker-compose.yml文件示例 1.mysql主从复制的docker-compose.yml文件 # cat docker-compose.yml version: '2' # 这个ve ...
- SharePoint2013使用资源管理器打开失败
我们在使用sharepoint 2013的文档库或者资源库的时候,经常会需要用到使用“资源管理器”来管理文档,但是有时候,点击“使用资源管理器打开”,会提示如下错误: 在文件资源管理器中打开此位置时遇 ...
- Dubbo2.7源码分析-SPI的应用
SPI简介 SPI是Service Provider Interface的缩写,即服务提供接口(翻译出来好绕口,还是不翻译的好),实质上是接口,作用是对外提供服务. SPI是Java的一种插件机制,可 ...
- 任务四十一:UI组件之日历组件(二)
任务四十一:UI组件之日历组件(二) 面向人群: 有一定基础的同学 难度: 中 重要说明 百度前端技术学院的课程任务是由百度前端工程师专为对前端不同掌握程度的同学设计.我们尽力保证课程内容的质量以及学 ...
- 一张图看懂Sprint Planning Meeting
本文主要参考<Scrum精髓>这本书的内容 每个Sprint都是从Sprint Planning Meeting开始,Scrum团队成员聚集在一起商定下个Sprint目标,并且确定在Spr ...
- linux使用mail命令发送邮件
在工作中使用linux,偶尔也会需要使用mail命令来进行发邮件. 从上面的命令看,系统已经安装了mail,对些我们还需要设置一下mail,让它使用外面的邮箱进行发邮件.设置文件是 /etc/m ...
- Spring Security 源码解析(一)
上篇 Spring Security基本配置已讲述了Spring Security最简单的配置,本篇将开始分析其基本原理 在上篇中可以看到,在访问 http://localhost:18081/use ...
- Java多线程学习之线程的取消与中断机制
任务和线程的启动很容易.在大多数情况下我们都会让他们运行直到结束,或是让他们自行停止.但是,有时我们希望提前结束任务或是线程,可能是因为用户请求取消,或是线程在规定时间内没有结束,或是出现了一些问题迫 ...