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 ...
随机推荐
- node 升级
npm install -g n npm update –g
- Linux环境下搭建Git仓库
1.安装Git $ yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel $ yum ...
- 【转】简明 Python 教程
原文网址:http://woodpecker.org.cn/abyteofpython_cn/chinese/ 简明 Python 教程Swaroop, C. H. 著沈洁元 译www.byteof ...
- hadoop之 HDFS fs 命令总结
版本:Hadoop 2.7.4 -- 查看hadoop fs帮助信息[root@hadp-master sbin]# hadoop fsUsage: hadoop fs [generic option ...
- rails里面添加妹子ui
妹子ui看起来很不错,以为在rails里面添加自定义的css和js和平时一样,结果可想而知,不过弄完以后发现还是比较简单的,这里记录一下 妹子ui需要加载的css和js如下 http://cdn.am ...
- 解决win下无法ping通VM虚拟机CentOS系统的方法
事情描述:公司迁新址,电脑带过去之后,用xshell连接vm的centos系统老是连接失败,然后考虑到公司迁新址这个情况,我首先怀疑是ip的问题,然后在vm中执行ifconfig找到centos的ip ...
- flv格式详解+实例剖析
简介 FLV(Flash Video)是现在非常流行的流媒体格式,由于其视频文件体积轻巧.封装播放简单等特点,使其很适合在网络上进行应用,目前主流的视频网站无一例外地使用了FLV格式.另外由于当前浏览 ...
- java代码---charAt()和toCharry()的用法
总结:charAt()是返回字符型,把字符串拆分成某个字符,返回指定位置的字符.可以把字符串看成char型数组 package com.sads; ///输出一个大写英文字母的 public clas ...
- java代码----I/O流从控制台输入信息判断并抛出异常
package com.a.b; import java.io.*; public class Yu { public static void main(String[] args) throws I ...
- 操作系统-服务器-百科:Windows Server
ylbtech-操作系统-服务器-百科:Windows Server Windows Server是微软在2003年4月24日推出的Windows 的服务器操作系统,其核心是Microsoft Win ...