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/ 题目地 ...
随机推荐
- Eclipse 中选中一个单词 ,其他相同的单词颜色就会变化
"Window"-"preferences"-"Java"-"Editor"-"Mark Occurrence ...
- Circular dependency issuse on cocoapods version(0.36.0) 全然解决方式(非降版本号)
此前的文章中.以前提到在cocoapods依赖管理版本号假设超过0.34.4.比方0.35, 0.36.0等版本号中. 运行"pod install" 或者 "pod u ...
- python之模块csv之 读取CSV文件(reader和DictReader2个方法)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #读取CSV文件(reader和DictReader2个方法) import csv #csv文件,是一种常用 ...
- 微信小程序条码、二维码生成模块
代码地址如下:http://www.demodashi.com/demo/13994.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...
- HDUOJ---2112HDU Today
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 在python中使用静态方法staticmethod
静态方法: 静态方法是类中的函数,不需要实例.静态方法主要是用来存放逻辑性的代码,主要是一些逻辑属于类,但是和类本身没有交互,即在静态方法中,不会涉及到类中的方法和属性的操作.可以理解为将静态方法存在 ...
- HawkHost老鹰主机更换主域名方法
http://www.yd631.com/change-hawkhost-primary-domain/圣诞节优惠期间,很多童鞋们购买了老鹰主机,可能由于大家初次使用海外主机或者是CP面板的空间.购买 ...
- iOS - App 与外设间的通信方式
1.前言 一般 iOS 开发者做 App 开发大部分时候都是通过 Http(s) 请求跟后台服务器打交道,做一些信息展示和用户交互.很少涉及到去跟外部硬件设备连接的开发.随着近年来车联网和物联网的兴起 ...
- IIS6.0中布署MVC站点
http://www.cnblogs.com/anny-1980/articles/2616657.html
- OGG_GoldenGate数据库配置DDL同步(案例)
2014-03-08 Created By BaoXinjian