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

  1. class Solution {
  2. public:
  3. vector<int> searchRange(vector<int>& nums, int target) {
  4. leftBinarySearch(nums,-,nums.size()-,target);//start始终在<target的位置
  5. if(result[]!=-) rightBinarySearch(nums,,nums.size(),target);//end始终在>target的位置
  6. return result;
  7. }
  8.  
  9. void leftBinarySearch(vector<int>& nums, int start, int end, int target){
  10. if(start+==end){ //结束条件:只剩两个数(因为此时mid==start,会进入死循环)
  11. if(target == nums[end]){
  12. result.push_back(end);
  13. }
  14. else {
  15. result.push_back(-);
  16. result.push_back(-);
  17. }
  18. return;
  19. }
  20.  
  21. int mid = start + ((end-start)>>);
  22. if(target <= nums[mid]) leftBinarySearch(nums,start,mid,target);
  23. else leftBinarySearch(nums,mid, end,target); //start始终在<target的位置
  24. }
  25.  
  26. void rightBinarySearch(vector<int>& nums, int start, int end, int target){
  27. if(start+==end){
  28. //must have one answer, so don't need if(target == nums[start])
  29. result.push_back(start);
  30. return;
  31. }
  32.  
  33. int mid = start + ((end-start)>>);
  34. if(target < nums[mid]) rightBinarySearch(nums,start,mid,target); //end始终在>target的位置
  35. else rightBinarySearch(nums,mid, end,target);
  36. }
  37. private:
  38. vector<int> result;
  39. };

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. ballerina 学习十一 Packages

    ballerina 的包还是比较简单的,实际上就是对于源码文件集合的管理,同时我们可以添加别名,同时可以进行 其他包的引用 import 简单例子 代码 import ballerina/math; ...

  2. spring加载bean报错:expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

    看具体报错日志: 警告: Unable to proxy interface-implementing method [public final void cn.wlf.selection.proto ...

  3. Vue.js实现数据的双向数据流

    众所周知,Vue.js一直使用的是单向数据流的,和angularJs的双向数据流相比,单向数据流更加容易控制.Vue.js允许父组件通过props属性传递数据到子组件.但是有些情况下我们需要在子组件里 ...

  4. 你一定想知道的关于FPGA的那些事

    首先,如果您从未接触过FPGA(现场可编程门阵列),或者有过一点基础想要继续深入了解这个行业,在这里,会向您介绍FPGA,并且向您解释FPGA都能解决什么问题,如何解决这些问题,并讨论如何将设计进行优 ...

  5. app的apk 安装的方法--adb--命令安装 (含把apk放某个文件夹,每次启动自己安装)

    adb安装 1.在app自动化之前,首先手机上有要被测试的app,如何把电脑本地上的app安装到手机上呢?可以在运行自动化代码前,在cmd输入adb指令,把电脑app安装到手机上 adb instal ...

  6. solr核心概念、配置文件

    Document Document是Solr索引(动词,indexing)和搜索的最基本单元,它类似于关系数据库表中的一条记录,可以包含一个或多个字段(Field),每个字段包含一个name和文本值. ...

  7. nginx 作为反向代理实现负载均衡的例子

    以下我们就来举例说明如何使用 nginx 实现负载均衡.因为nginx在处理并发方面的优势,现在这个应用非常常见.nginx 这个轻量级.高性能的 web server 主要可以干两件事情: 〉直接作 ...

  8. GSON使用笔记

    GSON简介 GSON是Google开发的Java API,用于转换Java对象和Json对象,我在这里将记录一下GSON的简单使用 下载GSON 我们可以在其github仓库中下载,也可以使用Mav ...

  9. Thread pools & Executors

    Thread pools & Executors Run your concurrent code in a performant way All about thread pools # H ...

  10. bbbbb