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. Content-Length实体的大小

    •15.2 Content-Length实体的大小 Content-Length首部指出了报文中实体主体的字节大小,这个大小包含了所有内容的编码,如对文本进行gzip压缩的话,那么Content-Le ...

  2. reload基础

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #reload基础 #与import和from的不同之处: #reload是python的内置函数,而不是语句 ...

  3. java中相同名字不同返回类型的方法

    这种名字相同返回类型不同的方法,在同一个类中是无法共存的,不论是继承过来的方法,还是多实现过来的方法,在一个类内都无法共存.名字确定了,你能改的只有参数(重载).

  4. FZU - 2039 Pets (二分图匹配 2011年全国大学生程序设计邀请赛(福州))

    Description Are you interested in pets? There is a very famous pets shop in the center of the ACM ci ...

  5. JS正则判断输入框是否仅仅含有汉字、字母和数字

    代码如下: if($.trim($("#user_api_register_form").find("input[name='user_name']").val ...

  6. awk 取列后对数值进行判断取出大于1的数值

    [root@dataline-prod nginx]# tail -2 access.log 122.238.119.177 - - [26/Oct/2018:18:20:25 +0800] &quo ...

  7. 从gentoo回归Arch,上组图

    Arch一直在我笔记本里边,只是玩gentoo时我不进Arch了,现在回归Arch,升级到了最新,用上了gentoo的最新的2.6.31内核(自己配置,无initrd),引导程序用的grub4dos: ...

  8. Python的copy()与deepcopy()区别

    Python的copy()与deepcopy()分别对应浅拷贝和深拷贝. 它们的理论区别: deepcopy():深复制(也就是寻常意义上的复制),即将被复制对象完全再复制一遍作为独立的新个体单独存在 ...

  9. [转]Splay Tree

    转自:http://blog.sina.com.cn/s/blog_7c4c33190100sg9r.html Splay Tree(又叫伸展树)本质上也是一棵二叉查找树.它不是严格平衡的,但通过一种 ...

  10. 【Linux】X window与文本模式的切换

    Linux默认的情况下会提供六个Terminal来让使用者登陆,切换的方式为:[Ctrl] + [Alt] + [F1]~[F6]的组合按钮.那这六个终端接口如何命名呢,系统会将[F1] ~ [F6] ...