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% ...
随机推荐
- webSQL的基本操作
1.html5以来,数据的前端存储已经有了很大进步.这里简单些一下webSQL的基本用法.代码如下: <!DOCTYPE html> <html> <head> & ...
- Struts文件上传(FormFile)
Struts中FormFile用于文件进行上传 1.在jsp文件中进行定义 <form action="/StrutsFileUpAndDown/register.do" m ...
- Java多态的一些陷阱
Java多态是如何实现的? Java的多态和C++一样,是通过延时绑定(late binding)或者说运行时绑定(runtime binding)来实现的.当调用某一个对象引用的方法时,因为编译器并 ...
- vue view 表单验证正常逻辑
<template> <Form ref="formInline" :model="formInline" :rules="rule ...
- Lucene系列-facet--转
https://blog.csdn.net/whuqin/article/details/42524825 1.facet的直观认识 facet:面.切面.方面.个人理解就是维度,在满足query的前 ...
- ruby中Regexp用法
Regexp 正则表达式的类.正则表达式的字面值是以双斜线内夹表达式的形式生成的. /^this is regexp/ 还可以使用Regexp.new(string)来动态地生成正则表达式对象. 超类 ...
- 【LeetCode题解】9_回文数(Palindrome-Number)
目录 9_回文数(Palindrome-Number) 描述 解法一:转化为字符串的比较 思路 Java 实现 Python 实现 复杂度分析 解法二:反转数字的后半部分 ★ 思路 Java 实现 P ...
- NPOI读取Excel官方demo
关键代码: 读: HSSFWorkbook hssfworkbook; void InitializeWorkbook(string path) { //read the template via F ...
- C# linq to xml 简单示例
data.xml <?xml version="1.0" encoding="utf-8" ?> <Data> <Products ...
- linux的文件基本属性
Linux系统是一种典型的多用户系统,不同的用户处于不同的地位,拥有不同的权限.为了保护系统的安全性,Linux系统对不同的用户访问同一文件(包括目录文件)的权限做了不同的规定 1.在Linux中我们 ...