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/ 题目地 ...
随机推荐
- TestNG方法測试及注意要点 代码及配置具体解释(解决testng方法不运行问题)
教你解决为什么TestNG中方法加了@Test注解,也在配置文件里配置了,可是方法就是不运行! 在使用TestNG进行測试时,使用配置文件的方式更easy于维护.可是常常遇到明明方法写了也配置运行了, ...
- 【Oracle】性能优化
优化原则 1.在select语句中避免使用* 2.使用Truncate清空表 2.1语法 Truncate [table|cluster] shema.[table_name][cluster_nam ...
- linux 2>&1的用法
linux中有三种标准输入输出,分别是STDIN,STDOUT,STDERR,对应的数字是0,1,2.STDIN就是标准输入,默认从键盘读取信息:STDOUT是标准输出,默认将输出结果输出至终端,也就 ...
- MSSQL-SQL SERVER 分页原理
项目中用到的, 用心琢磨一下此SQL语句即可: SELECT TOP $row * FROM ( SELECT ROW_NUMBER() OVER (ORDER BY [ID] desc ...
- Flyod 算法(两两之间的最短路径)
Flyod 算法(两两之间的最短路径)动态规划方法,通过相邻矩阵, 然后把最后的结果存在这么一个矩阵里面,(i,j), #include <iostream> #include <v ...
- poj-------Common Subsequence(poj 1458)
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 34477 Accepted: 13 ...
- iOS开发Xcode8需要注意的那些坑
现在在苹果的官网上,我们已经可以下载到Xcode8的GM版本了,加上9.14日凌晨,苹果就要正式推出iOS10系统的推送了,在此之际,iOS10的适配已经迫在眉睫啦,不知道Xcode8 beat版本, ...
- 竞态条件与sigsuspend函数
一.利用pause和alarm函数实现sleep函数 #include <unistd.h> int pause(void); pause函数使调用进程挂起直到有信号递达.如果信号的处理动 ...
- Unix环境高级编程(十一)线程
一个进程在同一时刻只能做一件事情,线程可以把程序设计成在同一时刻能够做多件事情,每个线程处理各自独立的任务.线程包括了表示进程内执行环境必需的信息,包括进程中标识线程的线程ID.一组寄存器值.栈.调度 ...
- Python 字典 values() 方法
描述 Python 字典 values() 方法以列表形式(并非直接的列表,若要返回列表值还需调用list函数)返回字典中的所有值. 语法 values() 方法语法: D.values() 参数 无 ...