Search for a Range

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

由于O(logn)时间要求,显然用二分查找。

思路是先用二分查找找到其中一个target,找不到则返回默认的ret值[-1, -1]

找到之后从这个位置往两边递归进行二分查找进行范围的拓展。

具体来说,ret[0]不断向左扩展,ret[1]不断向右扩展。

class Solution {
public:
vector<int> searchRange(int A[], int n, int target) {
vector<int> ret(, -);
int left;
int right;
int low = ;
int high = n-;
while((left = binarySearch(A, low, high, target)) != -)
{
ret[] = left;
high = left-;
}
low = ;
high = n-;
while((right = binarySearch(A, low, high, target)) != -)
{
ret[] = right;
low = right+;
}
return ret;
}
int binarySearch(int A[], int left, int right, int target)
{
while(left <= right)
{
int mid = left + (right-left) / ;
if(A[mid] == target)
return mid;
else if(A[mid] < target)
left = mid + ;
else
right = mid - ;
}
return -;
}
};

【LeetCode】34. Search for a Range的更多相关文章

  1. 【一天一道LeetCode】#34. Search for a Range

    一天一道LeetCode系列 (一)题目 Given a sorted array of integers, find the starting and ending position of a gi ...

  2. [Leetcode][Python]34: Search for a Range

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...

  3. 【LeetCode】74. Search a 2D Matrix

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Write an efficient algorithm that searches f ...

  4. LeetCode OJ 34. Search for a Range

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

  5. 【LeetCode题意分析&解答】34. Search for a Range

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

  6. 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...

  7. 【LeetCode】702. Search in a Sorted Array of Unknown Size 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 二分查找 日期 题目地址:https://lee ...

  8. 【LeetCode】34. Find First and Last Position of Element in Sorted Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...

  9. 【LeetCode】81. Search in Rotated Sorted Array II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/search-in ...

随机推荐

  1. 内存及字符串操作篇strlen strchar strcmp strcoll strcpy strdup strstr strtok strspn strrchr bcmp bcopy bzero index memccpy memset

    bcmp(比较内存内容) 相关函数 bcmp,strcasecmp,strcmp,strcoll,strncmp,strncasecmp 表头文件 #include<string.h> 定 ...

  2. 【C++ Primer】用于大型程序的工具

    1. 异常处理 异常以类似于将实參传递给函数的方式抛出和捕获.异常可以是可传给非引用实參的随意实參的类型,这意味着必须可以复制该类型的对象. 当抛出一个表达式的时候,被抛出对象的静态编译时类型将决定异 ...

  3. AS 代码模板 文件模板 Templates MD

    修改 File and Code Templates Settings –> Editor –>[File and Code Templates] 或者在右键new时选择子菜单[Edite ...

  4. IOS之导航控制器

    UINavigationController是用于构建分层应用程序的主要工具,主要采用栈形式来实现视图.任何类型的视图控制器都可放入栈中.在设计导航控制器时需要指定根视图即用户看到的第一个视图.根视图 ...

  5. Java中正则匹配性能测试

    工作中经常会用到在文本中每行检索某种pattern,刚才测试了三种方式,发现实际性能和预想的有区别 方式1: 直接字符串的matches方法,[string.matches("\\d+&qu ...

  6. python 插入数据获取id

    python 插入数据获取id 学习了:https://blog.csdn.net/qq_37788558/article/details/78151972 commit之前获取 cursor.las ...

  7. Cognos访问权限之让拒绝更友善

    关于cognos的访问权限之前我也做了不少总结,但是由于时间关系加上用户也只要实现功能就好,我们做的效果就是像很多人一样,就那样就好了.但是有很多事情,只要你肯动脑筋,你会发现,你还可以做的更好,下面 ...

  8. COM中的几个基本概念

    类厂 组件结构示例 DllGetClassObject COM库与类厂的交互

  9. [Javascipt] Immediately-Invoker 2

    Now the people at Poplar Puzzles would like you to treat an array of functions like a Queue, passing ...

  10. 基于Mongodb进行分布式数据存储

    http://blog.csdn.net/daizhj/article/details/5868360 注:本文是研究Mongodb分布式数据存储的副产品,通过本文的相关步骤可以将一个大表中的数据分布 ...