34. Search for a Range (Array; Divide-and-Conquer)
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].
思路:先二分法找最左端,再二分法找最右端。保证稳定排序。具体实现:
- 相同元素返回最左元素:start从-1开始,且总是在<target位置,最后会是最左侧=target元素之前的那个位置
- 相同元素返回最右元素: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)的更多相关文章
- [array] leetcode - 34. Search for a Range - Medium
leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...
- [Leetcode][Python]34: Search for a Range
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...
- [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 ...
- 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 ...
- 【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 ...
- 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 ...
- 34. Search for a Range
题目: Given a sorted array of integers, find the starting and ending position of a given target value. ...
- 【LeetCode题意分析&解答】34. Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- LeetCode 34. Search for a Range (找到一个范围)
Given an array of integers sorted in ascending order, find the starting and ending position of a giv ...
随机推荐
- [LeetCode系列]N皇后问题递归解法 -- 位操作方式
N皇后问题: 给定8*8棋盘, 放置n个皇后, 使其互相不能攻击(即2个皇后不能放在同一行/列/正反对角线上), 求解共有多少种放置方式? 这个问题的解答网上有不少, 但是位操作解法的我看到的不多. ...
- ecmall挂件开发实例二(转)
下述例子讲述了快速增加挂件的方法,但对系统中的代码未做更改,基本参照image_ad挂件的方法. 第 一步: 了解首页模板结构 首页文 件结构 include hearder.html 包含的头文件, ...
- 树的遍历——pat1043
http://pat.zju.edu.cn/contests/pat-a-practise/1043 给予N个数字组成二叉搜索树,判断这个数列是否由先序遍历得出或是镜像先序遍历得出,若是则输出相应的后 ...
- FPGA的新变化
FPGA SoC通过融合FPGA和ASIC两者的元件,跨越了灵活性和性能之间的界限.但随着它们进入高安全性.任务关键型市场,它们也面临着与标准SoC相同的问题,包括在日益复杂的器件中快速传输越来越多的 ...
- 使用maven打包额外的jar
当使用maven打包的时候,部分自己的额外使用的在maven基础库里面是没有的时候: 参考: https://blog.csdn.net/hguisu/article/details/5107268 ...
- 【CSS】文字超出显示省略号&连续字符换行
方法1.多行控制(css3) .text { width: 100%; word-break: break-all; display: -webkit-box; -webkit-line-clamp: ...
- kafka 经典教程
一.基本概念 介绍 Kafka是一个分布式的.可分区的.可复制的消息系统.它提供了普通消息系统的功能,但具有自己独特的设计. 这个独特的设计是什么样的呢? 首先让我们看几个基本的消息系统术语: Kaf ...
- struts2学习(4)struts2核心知识III
一.result配置: result - name 就是前面返回的值,success,error等. type: dispatcher. 默认是这个,底层是jsp的forward: redirect: ...
- Java中的自动类型转换/隐式类型转换
整型.实型(常量).字符型数据可以混合运算.运算中,不同类型的数据先转化为同一类型,然后进行运算. 转换从低级到高级. 自动类型转换必须满足转换前的数据类型的位数要低于转换后的数据类型,例如: sho ...
- zookeeper的四种类型的节点
znode创建类型(CreateMode),有以下四种: PERSISTENT 持久化节点 PERSISTENT_SEQUENTIAL 顺序自动编号持久化节点,这种节点会根据当前已存在的节点数自动加 ...