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% ...
随机推荐
- Mac下TensorFlow安装及环境搭建
在学习了一段时间台大李宏毅关于deep learning的课程,以及一些其他机器学习的书之后,终于打算开始动手进行一些实践了. 感觉保完研之后散养状态下,学习效率太低了,于是便想白天学习,晚上对白天学 ...
- 图形学 shader教程推荐
https://www.bilibili.com/video/av37119580 http://edu.manew.com/my/course/96 http://edu.manew.com/my ...
- Spanner:谷歌新一代全球部署的列式数据库
Spanner 是一个可扩展的.全球分布式的数据库,提供分布式ACID. 架构 universe:一个部署的实例成为universe,目前谷歌有3个,分别为开发/测试/线上 Zone:一个数据中心,相 ...
- 使用Koa2搭建web项目
随着Node.js的日益火热,各种框架开始层出不穷的涌现出来,Node.js也开始逐渐的被应用到处理服务端请求的场景中.搭建Web项目的框架也随之开始出现——express.koa.koa2.egg等 ...
- Low Power之CPF/UPF
1 CPF The Common Power Format is a standard promoted by the Low Power Coalition at Si2. CPF is also ...
- 【LeetCode题解】141_环形链表
目录 141_环形链表 描述 解法一:哈希表 思路 Java 实现 Python 实现 解法二:双指针(龟兔算法) 思路 Java 实现 Python 实现 141_环形链表 描述 给定一个链表,判断 ...
- XRP节点部署
目录 XRP节点部署 准备 硬软件配置(建议) 安装Rippled服务 一. 以Stock Server模型运行 在何种情况下运行此模式 二 .以 Validator模式运行 在何种情况下运行此模式 ...
- Hive是什么
Hive是什么1)Hive 是建立在Hadoop (HDFS/MR)上的用于管理和查询结果化/非结构化的数据仓库:2)一种可以存储.查询和分析存储在Hadoop 中的大规模数据的机制:3)Hive 定 ...
- BATJ面试必会之并发篇
一.线程状态转换 新建(New) 可运行(Runnable) 阻塞(Blocking) 无限期等待(Waiting) 限期等待(Timed Waiting) 死亡(Terminated) 二.使用线程 ...
- Qt5 编程基础
Qt 是一个C++ GUI应用框架,Qt 具有良好的可移植性支持大多数桌面和移动操作系统并常用于嵌入式开发. Qt的发行版分为商业版和开源版,提供了Qt Creator作为轻量级IDE. Hello ...