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

思路:先二分法找最左端,再二分法找最右端。保证稳定排序。具体实现:

  1. 相同元素返回最左元素:start从-1开始,且总是在<target位置,最后会是最左侧=target元素之前的那个位置
  2. 相同元素返回最右元素:end总是在>target位置,所以从n开始,最后会是最右侧=target元素之后的那个位置

结束条件:start+1==end

class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
leftBinarySearch(nums,-,nums.size()-,target);//start始终在<target的位置
if(result[]!=-) rightBinarySearch(nums,,nums.size(),target);//end始终在>target的位置
return result;
} void leftBinarySearch(vector<int>& nums, int start, int end, int target){
if(start+==end){ //结束条件:只剩两个数(因为此时mid==start,会进入死循环)
if(target == nums[end]){
result.push_back(end);
}
else {
result.push_back(-);
result.push_back(-);
}
return;
} int mid = start + ((end-start)>>);
if(target <= nums[mid]) leftBinarySearch(nums,start,mid,target);
else leftBinarySearch(nums,mid, end,target); //start始终在<target的位置
} void rightBinarySearch(vector<int>& nums, int start, int end, int target){
if(start+==end){
//must have one answer, so don't need if(target == nums[start])
result.push_back(start);
return;
} int mid = start + ((end-start)>>);
if(target < nums[mid]) rightBinarySearch(nums,start,mid,target); //end始终在>target的位置
else rightBinarySearch(nums,mid, end,target);
}
private:
vector<int> result;
};

34. Search for a Range (Array; Divide-and-Conquer)的更多相关文章

  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. [Leetcode][Python]34: Search for a Range

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

  3. [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 ...

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

  5. 【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 ...

  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. 34. Search for a Range

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

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

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

  9. LeetCode 34. Search for a Range (找到一个范围)

    Given an array of integers sorted in ascending order, find the starting and ending position of a giv ...

随机推荐

  1. 使用distillery 构建专业的 phoenix 项目软件包

    备注:     首先需要安装 elixir 环境   1. 基本项目说明 参考项目: https://github.com/rongfengliang/phoenix-rest-demo 2. 项目说 ...

  2. Nchan 实时消息 安全配置

    备注:     即时消息的安全对于我们来说是比较重要的,作者在设计Nchan 的时候已经考虑了 a. nchan_authorize_request (Hooks and Callbacks)可以集成 ...

  3. java之反射概述

    类加载器和反射  类加载器: 1 类的加载过程: 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载.连接.初始化三步骤来实现对这个类进行初始化. 加载:就是指将class文件读入内存 ...

  4. php mysql apache 的字符集

    在使用中常常遇到utf-8和utf8,现在终于弄明白他们的使用不同之处了,现在来和大家分享一下,下面我们看一下utf8 和 UTF-8 有什么区别 “UTF-8”是标准写法,php在Windows下边 ...

  5. ubuntu :扩充虚拟机的磁盘容量

    前言: 开始建立虚拟机的时候给的容量是20G,给了10G的交换空间,所以后来有点不够用了,现在安装软件会出现提示磁盘空间不足,所以需要扩充一下磁盘的容量. 步骤:     1.因为我用的是Vmware ...

  6. Unit07: MyBatis框架简介 、 MyBatis基本应用

    Unit07: MyBatis框架简介 . MyBatis基本应用 1. myBatis (1)myBatis是什么? 是一个开源的持久层框架. 注:myBatis底层仍然是jdbc. (2)编程步骤 ...

  7. 关于PIPELINE,也许你应该知道

    所谓流水线(pipeline)设计,通常来说是流水生产线. 同样,组合逻辑路径可以看作是一条生产线,路径上的每个逻辑单元都可以看作是一个阶段,都会产生时延. 如果不采用流水设计,前后级组合逻辑依次工作 ...

  8. nginx限制请求之四:目录进行IP限制

    相关文章: <高可用服务设计之二:Rate limiting 限流与降级> <nginx限制请求之一:(ngx_http_limit_conn_module)模块> <n ...

  9. ECMAScript有6种继承方式(实现继承)

    本人对于ECMAScript继承机制有些见解,如果说的不对,敬请赐教~~~~ 继承是OO语言(面向对象)挺好的概念,许多OO语言都支持两种继承方式(接口只继承方法签名.实际继承则继承实际的方法),但是 ...

  10. 【BZOJ】1926: [Sdoi2010]粟粟的书架(暴力+主席树)

    题目 传送门:QWQ 分析 两道题目 第一问暴力预处理 用$ a[i][j][k] $和$ s[i][j][k] $ 表示从$ (1,1) $ 到 $ (i,j) $ 这个矩形中比k大的数的个数和这些 ...