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. 关于shared pool的深入探讨(一) 【转载】

    关于shared pool的深入探讨(一)   作者:eygle |English [转载时请标明出处和作者信息]|[恩墨学院 OCM培训传DBA成功之道]链接:http://www.eygle.co ...

  2. Bytes to be written to the stream exceed the Content-Length bytes size specified 解决方法

    context.Response.ContentType = encode;                using (StreamWriter writer = new StreamWriter( ...

  3. POJ - 3662 Telephone Lines (Dijkstra+二分)

    题意:一张带权无向图中,有K条边可以免费修建.现在要修建一条从点1到点N的路,费用是除掉免费的K条边外,权值最大的那条边的值,求最小花费. 分析:假设存在一个临界值X,小于X的边全部免费,那么此时由大 ...

  4. 【Head First Servlets and JSP】笔记 25:JSTL 参考

    <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ t ...

  5. qplot()函数的详细用法

    qplot()函数的详细用法: library(ggplot2) # 测试数据集,ggplot2内置的钻石数据qplot(carat, price, data = diamonds)dsmall &l ...

  6. Python3.x:pyodbc调用sybase的存储过程

    Python3.x:pyodbc调用sybase的存储过程 示例代码 # python3 # author lizm # datetime 2018-03-02 17:00:00 # -*- codi ...

  7. Keepalived + Mysql 主主复制高可用

    环境 系统:Centos 7.4 x64 服务:Mariadb 5.5 .Keepalived 1.3.5.6  结构 主1:192.168.1.108 主2:192.168.1.109 VIP:19 ...

  8. 黑苹果Yosemite 10.10.1懒人版完美安装及简单驱动设置

    1.硬件概要 CPU: 英特尔 Xeon E3-1230 V2 (四核)主板: 技嘉 H77-DS3H (Intel H77 (Panther Point Base))内存: 8 GBytes显卡: ...

  9. 判断一个对象是否有new

    C++语言中,对象没有空和不空的概念,只有对象指针才有空和不空的概念 判断对象指针是否为空只需要和NULL常量进行比较即可 如果相等,则为空,否则不为空 另外对象虽然没有空和不空的概念,但是有有效和无 ...

  10. javascript-电话薄小功能

    上代码: <style> *{ margin:0 ; padding: 0;} .phrase_wrap, .phrase_list{ width: 200px; height: 200p ...