LeetCode: Search for a Range 解题报告

Search for a Range
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].
SOLUTION 1:
使用改进的二分查找法。终止条件是:left < right - 1 这样结束的时候,会有2个值供我们判断。这样做的最大的好处是,不用处理各种越界问题。
感谢黄老师写出这么优秀的算法:http://answer.ninechapter.com/solutions/search-for-a-range/
请同学们一定要记住这个二分法模板,相当好用哦。
1. 先找左边界。当mid == target,将right移动到mid,继续查找左边界。
最后如果没有找到target,退出
2. 再找右边界。当mid == target,将left移动到mid,继续查找右边界。
最后如果没有找到target,退出
public class Solution {
public int[] searchRange(int[] A, int target) {
int[] ret = {-, -};
if (A == null || A.length == ) {
return ret;
}
int len = A.length;
int left = ;
int right = len - ;
// so when loop end, there will be 2 elements in the array.
// search the left bound.
while (left < right - ) {
int mid = left + (right - left) / ;
if (target == A[mid]) {
// 如果相等,继续往左寻找边界
right = mid;
} else if (target > A[mid]) {
// move right;
left = mid;
} else {
right = mid;
}
}
if (A[left] == target) {
ret[] = left;
} else if (A[right] == target) {
ret[] = right;
} else {
return ret;
}
left = ;
right = len - ;
// so when loop end, there will be 2 elements in the array.
// search the right bound.
while (left < right - ) {
int mid = left + (right - left) / ;
if (target == A[mid]) {
// 如果相等,继续往右寻找右边界
left = mid;
} else if (target > A[mid]) {
// move right;
left = mid;
} else {
right = mid;
}
}
if (A[right] == target) {
ret[] = right;
} else if (A[left] == target) {
ret[] = left;
} else {
return ret;
}
return ret;
}
}
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/divide2/SearchRange.java
LeetCode: Search for a Range 解题报告的更多相关文章
- 【LeetCode】632. Smallest Range 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/smallest ...
- LeetCode: Search a 2D Matrix 解题报告
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)
[LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】870. Advantage Shuffle 解题报告(Python)
[LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
随机推荐
- 关于linux的添加永久静态路由的static-routes方法
一:使用 route 命令添加 使用route 命令添加的路由,机器重启或者网卡重启后路由就失效了,方法: //添加到主机的路由 # route add –host 192.168.1.11 dev ...
- lenovo thinkpad L460 安装Win7阐述
lenovo thinkpad L460 系统自带Win10切GPT分区,如果想安装Win7 01.需要改bios 02.清空硬盘全盘数据,调整分区类型MBR分区引导(需要全盘格式化,调整引导类型) ...
- Centos7安装Openresty
通过yum安装 在 /etc/yum.repos.d/ 下新建 OpenResty.repo 内容 [openresty] name=Official OpenResty Repository bas ...
- easyui panel自适应问题
项目中要用到easyui,使用也有几年时间了,刚开始使用还不错,毕竟只是简单的增删改查数据,不过到后面越来越觉得easyui不如extjs了,好多复杂一点的问题,easyui表现就力不从心了,题外话就 ...
- 【laravel5.4】迁移文件的生成、修改、删除
建议直接去官方文档查看: https://laravel-china.org/docs/laravel/5.4/migrations#creating-columns 1.生成迁移: 主要方式:1.创 ...
- poj----Maximum sum(poj 2479)
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30704 Accepted: 9408 Desc ...
- 运行shell出错: 没有那个文件或目录
http://blog.163.com/zhangjie_0303/blog/static/99082706201136114548840/
- Unix环境高级编程(十九)终端I/O
终端I/O应用很广泛,用于终端.计算机之间的直接连线.调制解调器以及打印机等等.终端I/O有两种不同的工作模式: (1)规范模式输入处理:终端输入以行为单位进行处理,对于每个读要求,终端驱动程序最多返 ...
- Python chr() 函数
描述 chr() 用一个范围在 range(256)内的(就是0-255)整数作参数,返回一个对应的字符. 语法 以下是 chr() 方法的语法: chr(i) 参数 i -- 可以是10进制也可以是 ...
- swagger and restful api 参考
http://git.oschina.net/redArmy/spring-cloud-books/blob/master/spring-cloud-provider-book/src/main/ ...