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% ...
随机推荐
- Flow类中的resolveBreaks与resolveContinues
/** Resolve all continues of this statement. */ boolean resolveContinues(JCTree tree) { boolean resu ...
- Redis笔记(二):Redis数据类型
Redis 数据类型 Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合). String(字符串) st ...
- 数据库学习---SQL基础(一)
数据库学习---SQL基础(一) 数据库学习---SQL基础(二) 数据库学习---SQL基础(三) SQL(struct query language)结构化查询语言:一种专门与数据库通信的语言, ...
- MySQL 报错MySQL server syntax to use near 'OPTION SQL_SELECT_LIMIT=DEFAULT'
在hive的应用中,出现如下错误时You have an error in your SQL syntax; check the manual that corresponds to your MyS ...
- H5开发中遇到的问题及解决办法
记不得什么时候进行H5开发的学习了,只知道是从2016年8月1日开始修复Bug,计划每天把学到的东西以及遇到问题时候的解决方案都记录下来,希望自己能够坚持下去,每天积累一点,希望有所进步吧. 1.Th ...
- DataTable的Ajax使用
DataTable Datatables是一款jquery表格插件.它是一个高度灵活的工具,可以将任何HTML表格添加高级的交互功能. 官网: https://datatables.net/ 中文网: ...
- NOI2018 退役记
退役预订... upd 果然就这么不光荣的退役了... 我居然考出了一场只有两题得分的比赛,我好菜啊... 不过高三充(tui)实(fei)的生活应该很有意思... 大家一起加油吧!!!
- vue权限路由实现方式总结二
之前已经写过一篇关于vue权限路由实现方式总结的文章,经过一段时间的踩坑和总结,下面说说目前我认为比较"完美"的一种方案:菜单与路由完全由后端提供. 菜单与路由完全由后端返回 这种 ...
- insert into 语句的三种写法
insert into 语句的三种写法 方式1. INSERT INTO t1(field1,field2) VALUES (v001,v002); // 明确只插入一条Valu ...
- emberjs 按年月分组
一个集合,里面有年和月的属性,按照年和月进行分组显示数据 + item.TopicMonth }).map(function (value, key) { return { time: { year: ...