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. node.js---sails项目开发(6)--- 实现分页功能

    只需要添加一个文件即可  api/blueprints/find.js     代码如下 /** * Module dependencies */ var util = require('util') ...

  2. Linux vim 操作技巧

    ·Linux设计的重要原则是信息存储在基于文本的文件中 文本文件:无格式文件,作用类似于win的注册表(etc下的配置文件,.conf或者无扩展名)可扩展标记语言(XML),文本标记定义数据结构(et ...

  3. Java应用程序连接数据库--JDBC基础

    Java应用程序连接数据库--JDBC基础   Java应用程序连接数据库–JDBC基础 <!-- MySQL驱动,连接数据库用,由数据库厂商提供 --> <dependency&g ...

  4. Java学习之垃圾回收

    垃圾回收(GC) GC需要完成的三件事情: 哪些内存需要回收? 什么时候回收? 如何回收? 为什么"GC自动化"之后还要研究GC?当需要排查各种内存溢出.内存泄漏问题时,当GC成为 ...

  5. H3C交换机配置镜像端口

    配置步骤 进入配置模式:system-view: 创建本地镜像组:mirroring-group 1 local 为镜像组配置源端口:mirroring-group 1 mirroring-port ...

  6. 伸展树基础(Splay)

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3948  Solved: 1627 [Submit][St ...

  7. DFS - 深度搜索 - 基于邻接列表表示法

    2017-07-25 15:38:00 writer:pprp 在前一篇图基于邻接列表表示法的代码加了一小部分,加了一个DFS函数,visited[N]数组 参考书目:张新华的<算法竞赛宝典&g ...

  8. Service的理解

    转自 原文 Service的理解 Service是什么?Service是一种可以在后台运行相关任务的组件.没有界面.其存在的线程是主线程,一般会通过启动子线程来执行耗时操作.Service有什么用?可 ...

  9. nagios报错Error: No such CGI app - /usr/local/nagios/sbin/nagios/cgi-bin/status.cgi may not exist or is not executable by this process.

    加上rewrite rewrite ^/nagios/cgi-bin/(.*)\.cgi /$.cgi break;  

  10. mysql实际使用思路

    在熟悉mysql语法的基础上,想在自己的应用程序中使用它,应该怎么操作呢? 自然的想法就是找到相应语言的mysql接口,然后熟悉接口,对其进行调用. 具体的做法与思路如下: 找到C的mysql接口 新 ...