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. Maximum Likelihood Method最大似然法

    最大似然法,英文名称是Maximum Likelihood Method,在统计中应用很广.这个方法的思想最早由高斯提出来,后来由菲舍加以推广并命名. 最大似然法是要解决这样一个问题:给定一组数据和一 ...

  2. 3D几何图形生成的DEMO

    3D几何图形生成的DEMO 可以生成以下几种图形: [1] 平面(Plane)图形的生成算法 [2] 立方体(Box)图形的生成算法 [3] 球(Sphere)图形的生成算法 [4] 圆锥(Cone) ...

  3. 简单MapReduce思维导图

  4. iOS开发-照片选择

    本来想做个注册登录的表单的,想想还是先做个简单的头像选择,一般情况下不管是内部管理系统还是面向公众的互联网公司,注册登录是免不了的,用户头像上传是免不了的,尤其是企业用户,上传了自己的图片才感觉自己买 ...

  5. App优化 Systrace

    简介 trace [tres] vt.跟踪,追踪; 追溯,探索; 探索; 查找; n.痕迹; 痕迹,踪迹; 微量,极少量; 1 1   1 trace [tres] vt.跟踪,追踪; 追溯,探索; ...

  6. vim学习笔记(2)——vim配置

    记录vim的配置,随时更新 MacVim 安装: homebrew,安装位置:/usr/local/Cellar brew linkapps macvim--将macvim.app加入到Applica ...

  7. Android -- ImageLoader简析

    图片的内存缓存实现 Image-Loader库有一个较完整的内存缓存实现,使用者可以根据需要选择已经实现的策略,也可以定制自己项目中需要的策略. 内存缓存实现代码在memory和memory.impl ...

  8. 红米NOTE应用闪退(包括系统设置等各种界面)问题解决经历

    关机状态下同时按减音量按钮和开机按钮,进入一个菜单,选择recovery(减音量键是移动,加音量键是确定),当出现一个倒地的机器人的时候,稍待片刻按开机键会出现双清以及刷机菜单.wipe data/f ...

  9. C++ vector用法(转)

    在c++中,vector是一个十分有用的容器,下面对这个容器做一下总结. 1 基本操作 (1)头文件#include<vector>. (2)创建vector对象,vector<in ...

  10. (LeetCode 203)Remove Linked List Elements

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...