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 ...
随机推荐
- RBAC (基于角色的访问控制)
基于角色的访问控制(Role-Based Access Control)作为传统访问控制(自主访问,强制访问)的有前景的代替受到广泛的关注.在RBAC中,权限与角色相关联,用户通过成为适当角色的成员而 ...
- 一些IPC常用头文件
//my_err.h#include <errno.h> /* for definition of errno */ #include <stdarg.h> /* ISO C ...
- ecmall类关系图(转)
- Web 漏洞分析与防御之 XSS(一)
原文地址:Web 漏洞分析与防御之 XSS(一) 博客地址:http://www.extlight.com 一.全称 跨站脚本攻击(Cross Site Scripting) 二.原理 通过在网站中的 ...
- sql的一些事件处理
select getdate() select Convert(varchar(10),getdate(),120) yyyy-mm-ddselect Convert(varchar(20),getd ...
- Android Java访问本地方法(JNI)
当功能需要本地代码实现的时候,Java 代码就需要调用本地代码. 在调用本地代码时,首先要保证本地代码被加载到 Java 执行环境中并与 Java 代码连接在一起,这样 Java 代码在调用本地方法时 ...
- (新)解决php版本ueditor中动态配置图片URL前缀(imageurlprefix)的方法
昨天晚上写了一篇文章<解决ueditor中没法动态配置imageurlprefix的方法>,通过修改js获取当前域名的方法,配置imageurlprefix值: 发现还是不够灵活,因为域名 ...
- CentOS6.8编译安装LAMP
CentOS6.8编译安装Apache2.4.25.MySQL5.7.16.PHP5.6.29 初始化 #固定IP vi /etc/sysconfig/network-scripts/ifcfg-et ...
- java代码----数据类型的转换-----int --->String
总结:int ----->String package com.a.b; //测试..char--->int // int--->String public class Yue2 { ...
- [Java.web]Web应用结构
以Web应用放在 Tomcat\webapps\ 目录下为例 day01 目录 | |------------- html.jsp.css.js 文件等 |------------- ...