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 解题报告的更多相关文章

  1. 【LeetCode】632. Smallest Range 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/smallest ...

  2. 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 ...

  3. 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)

    [LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...

  4. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  5. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  6. 【LeetCode】886. Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  7. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  8. 【LeetCode】870. Advantage Shuffle 解题报告(Python)

    [LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

  9. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

随机推荐

  1. Oracle常用标准表

    一.INV(库存) 子库存:mtl_secondary_inventories 事物处理:mtl_material_transactions mmt 事务处理来源类型:mtl_txn_source_t ...

  2. 转:CMake安装和使用

      CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程).他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性,类似UNIX ...

  3. HDFS架构设计

    原文:http://hadoop.apache.org/docs/r2.6.4/hadoop-project-dist/hadoop-hdfs/HdfsDesign.html 介绍 HDFS是个分布式 ...

  4. MyEcplise安装Freemarker插件(支持.ftl文件)

    1.下载插件:http://sourceforge.net/projects/freemarker-ide/?source=typ_redirect 2.下载freemarker-2.3.19.jar ...

  5. springmvc+spring框架

    jar包 com.springsource.javax.validation-1.0.0.GA.jar com.springsource.org.aopalliance-1.0.0.jar com.s ...

  6. 史上最全的iOS面试题及答案,且看且珍藏,错过就没有喽!

    1. Object-c的类可以多重继承么?可以实现多个接口么?Category是什么?重写一个类的方式用继承好还是分类好?为什么? 答:Object-c的类不可以多重继承;可以实现多个接口,通过实现多 ...

  7. sudo: Cannot execute /usr/local/bin/zsh: No such file or directory 问题

    参考:sudo: Cannot execute /usr/local/bin/zsh: No such file or directory 之前在美化Ubuntu的时候,下了个zsh,但是忘记改配置文 ...

  8. Hadoop DistCp 使用指南

    原文地址:http://hadoop.apache.org/docs/r1.0.4/cn/distcp.html 概述 使用方法 基本使用方法 选项 选项索引 更新和覆盖 附录 Map数目 不同HDF ...

  9. Go 语言官方包函数中文翻译

    Go官方包函数中文翻译 *** import "strings" func Join(a []string, sep string) string Join concatenate ...

  10. 基于KWIC 的keyword匹配算法(管道+过滤器模式下实现)

    以下是基于KWIC 的keyword匹配算法(管道+过滤器模式下实现) 关键部分的管道+过滤器 软件体系下的实现, 在非常多的keyword搜索平台都使用了这一 循环移位+排序输出的 keyword匹 ...