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].

查找一个区间开头以及结尾的下标,分两次二分查找,一次向左一次向右即可,代码如下:

 class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
int sz = nums.size();
int left = bs(nums, , sz, target, true);
int right = bs(nums, , sz, target, false);
vector<int> res;
res.push_back(left);
res.push_back(right);
return res;
} int bs(vector<int>& nums, int beg, int end, int target, int goLeft)
{
if(beg > end)
return -;
int mid = (beg + end)/;
if(nums[mid] == target){
int tmpAns = (goLeft == true ? bs(nums, beg, mid - , target, goLeft) : bs(nums, mid + , end, target, goLeft));
return tmpAns == - ? mid : tmpAns;
}else if(nums[mid] < target){
return bs(nums, mid + , end, target, goLeft);
}else{
return bs(nums, beg, mid - , target, goLeft);
}
}
};

LeetCode OJ:Search for a Range(区间查找)的更多相关文章

  1. [array] leetcode - 34. Search for a Range - Medium

    leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...

  2. [OJ] Search for a Range

    LintCode 61. Search for a Range (Medium) LeetCode 34. Search for a Range (Medium) class Solution { p ...

  3. [LeetCode] 034. Search for a Range (Medium) (C++/Java)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...

  4. 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 ...

  5. [LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)

    原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an ...

  6. leetCode 34.Search for a Range (搜索范围) 解题思路和方法

    Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...

  7. leetcode 34 Search for a Range(二分法)

    Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...

  8. LeetCode 034 Search for a Range

    题目要求:Search for a Range Given a sorted array of integers, find the starting and ending position of a ...

  9. LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)

    题目链接: https://leetcode.com/problems/search-for-a-range/?tab=Description   Problem: 在已知递减排序的数组中,查找到给定 ...

  10. LeetCode Search for a Range (二分查找)

    题意 Given a sorted array of integers, find the starting and ending position of a given target value. ...

随机推荐

  1. 【转载】在Jersey JAX-RS 处理泛型List等Collection

    在Java中,从1.5开始,我们就可以使用泛型了(generic),这看上去很像C++ Template,但是实际上它们是不同的.在这里我不想过多的描述细节,你可以从Google上搜索一下. 但是,泛 ...

  2. 【转】记一次SQLServer的分页优化兼谈谈使用Row_Number()分页存在的问题

      最近有项目反应,在服务器CPU使用较高的时候,我们的事件查询页面非常的慢,查询几条记录竟然要4分钟甚至更长,而且在翻第二页的时候也是要这么多的时间,这肯定是不能接受的,也是让现场用SQLServe ...

  3. python中的闭包是什么

    当一个嵌套函数在其外部区域引用了一个值时,该嵌套函数就是一个闭包,其意义就是会记录这个值 def A(x): def B(): print(x) return B A(7)() #7

  4. JSON 弹窗

    JSON和AJAX <script type="text/javascript"> $(document).ready(function(e) { var a = { ...

  5. hadoop07---synchronized,lock

    synchronized 锁是jvm控制的,控制锁住的代码块只能有一个线程进入.线程执行完了锁自动释放,抛出异常jvm会释放锁. synchronized的缺陷 1.如果一个线程被阻塞了,其余的线程 ...

  6. xaml可扩展应用程序标记语言

    xaml 类似于 html,但不是html,它是基于xml语言的:’html可以呈现在浏览器中而xaml 可以现实 3d动画等特效. xaml  是强类型语言,  是解释性语言,虽然他可以被编译.

  7. <nginx+PHP>nginx环境下配置支持php7

    [root@redhat7 ~]# wget http://am1.php.net/get/php-7.1.2.tar.gz/from/this/mirror [root@redhat7 ~]# ta ...

  8. QMap的使用

    1.定义 mapQMap<QString,QColor> map; 2.插入数据 map.insert("AA",RGB(255,0,0)); map.insert(& ...

  9. 《Maven实战》第14章 灵活的构建

    面对不同环境的差异能够灵活的构建项目, 操作系统的差异 开发环境.测试环境.产品环境的差异(最常用) 不同客户的差异 Maven中灵活的构建:属性.资源过滤.profile 14.1Maven属性 6 ...

  10. 混合开发的大趋势之 一个Android程序员眼中的 React.js 块级作用域 和 let

    转载请注明出处:王亟亟的大牛之路 最近都有事干然后,快到月底了这个月给CSDN的博文也就两篇,想想也蛮多天没更了,那就来一篇. 老规矩,先安利:https://github.com/ddwhan012 ...