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. C# Socket Post File

    ///<summary> ///向服务器发送混合型的请求,1:成功发送,0:发送失败 ///</summary> ///<param name="paranam ...

  2. new String(tmp,1,nlen,"UTF8")

    tmp是一个byte(字节)数组,如:['a','b','c'...],tmp[0]是去byte中的第一个,运算符&表示按位运算‘且’,就是前后值的二进制相同位有0取0,否则取1,如:2&am ...

  3. Unit06: 外部对象概述 、 window 对象 、 document 对象

    Unit06: 外部对象概述 . window 对象 . document 对象 小代码演示: <!DOCTYPE html> <html> <head> < ...

  4. sprinboot+redis

    (1)pom.xml引入jar包,如下: <dependency> <groupId>org.springframework.boot</groupId> < ...

  5. java代码-----运用endWith()和start()方法

    总结: package com.a.b; //startWith().和endWith()是检查一个字符串是否以一个特定的字符序列开始或结束 public class Sdfs { public st ...

  6. zookeeper的四种类型的节点

    znode创建类型(CreateMode),有以下四种: PERSISTENT 持久化节点 PERSISTENT_SEQUENTIAL 顺序自动编号持久化节点,这种节点会根据当前已存在的节点数自动加 ...

  7. MongoDB day01

    MongoDB芒果数据库 数据存储阶段 文件管理阶段(.txt .doc .xlc) 优点:数据可以长期保存:数据有一定格式化规范:可以大量存储:使用简单方便 缺点:数据一致性差:用户查找修改不方便: ...

  8. 几个常用的Linux监控脚本

    几个常用的Linux监控脚本 几个常用的Linux监控脚本下面是几个主机监控的脚本,大家可以根据自己的情况再进行修改,希望能给大家一点帮助.1.查看主机网卡流量#!/bin/bash#network# ...

  9. Linux下sz下载文件超过4G办法

    当下载文件超过4G时,sz提示能下载. 解决办法有以下几种: 1.scp 2.ftp 3.nc 4.icmp 5.文件切割 ---------------------------------- 以上4 ...

  10. linux weather