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 [-, -].

For example,
Given [, , , , , ] and target value ,
return [, ].

Analysis: 这道题是二分查找Search Insert Position的变体,思路并不复杂,就是先用二分查找找到其中一个target,然后再往左右找到target的边缘。找边缘的方法跟二分查找仍然是一样的,只是相等的情况依然要往左找(找左边缘)或往右找(找右边缘)。这样下来总共进行了三次二分查找,所以算法的时间复杂度仍是O(logn),空间复杂度是O(1)。

Notice: 对停止的边界条件极不熟悉,需要总结,参见Binary Search的Summary

本题的精髓和难点在于寻找左右边沿,方法是Binary Search的变体,只不过这次是要左右相遇。以找左边缘为例,while循环里要么A[m] < target, l 跳到m+1, 要么A[m]==target, r 跳到m-1, 直到l > r为止,这时候l所在的位置就是要求的左边缘。而如果是找右边缘,l > r之后,r所在位置就是要求的右边缘。l和r所停的位置正好是目标数的后面和前面。

找左边沿可以认为是在找一个比target略小的目标数(因为等于target的时候移动的是右边缘),这个目标数在A中不存在,所以当循环结束时候,l, r分别处在目标数的后面和前面。如果我们找的是左边缘,那么这个左边缘应该是在目标数右边,所以就是l所处的位置

整体如果想用iterative的方法来写:

两次binary search, 一次找左边缘,一次找右边缘, be carefule of line 12, r有可能<0

 public class Solution {
public int[] searchRange(int[] nums, int target) {
int[] res = new int[]{-1, -1};
if (nums==null || nums.length==0) return res;
int l=0, r=nums.length-1;
int m = l + (r-l)/2;
while (l <= r) { // find right edge
m = l + (r-l)/2;
if (nums[m] <= target) l = m + 1;
else r = m - 1;
}
if (r<0 || nums[r] != target) return res;
else res[1] = r; l = 0;
//no need to set r = nums.length - 1, because r is already at right place
while (l <= r) {
m = l + (r-l)/2;
if (nums[m] < target) l = m + 1;
else r = m - 1;
}
res[0] = l;
return res;
}
}

Leetcode: Find First and Last Position of Element in Sorted Array的更多相关文章

  1. Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)

    本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...

  2. 乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array

    乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array 一.前言 这次我们还是要改造二分搜索,但是想法却 ...

  3. Find First and Last Position of Element in Sorted Array - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Find First and Last Position of Element in Sorted Array - LeetCode 注意点 nums可能 ...

  4. [LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)

    原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an ...

  5. leetcode-algorithms-34 Find First and Last Position of Element in Sorted Array

    leetcode-algorithms-34 Find First and Last Position of Element in Sorted Array Given an array of int ...

  6. 刷题34. Find First and Last Position of Element in Sorted Array

    一.题目说明 题目是34. Find First and Last Position of Element in Sorted Array,查找一个给定值的起止位置,时间复杂度要求是Olog(n).题 ...

  7. [LeetCode] 34. Find First and Last Position of Element in Sorted Array == [LintCode] 61. Search for a Range_Easy tag: Binary Search

    Description Given a sorted array of n integers, find the starting and ending position of a given tar ...

  8. (二分查找 拓展) leetcode 34. Find First and Last Position of Element in Sorted Array && lintcode 61. Search for a Range

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  9. [LeetCode] 34. Find First and Last Position of Element in Sorted Array 在有序数组中查找元素的第一个和最后一个位置

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

随机推荐

  1. C++——inline function

    前言 当代码写复杂后,一定会封装出大量的函数,这会导致两个问题: ①函数越多,栈的消耗也越厉害 疑问:为什么代码复杂了.函数变多了,栈消耗的就很厉害? 答:因为这会导致函数的调用深度可能会很深,比如: ...

  2. c++混合使用不同标准编译潜在的问题

    最近项目使用的C++的版本到C++11了,但是由于有些静态库(.a)没有源码,因此链接时还在使用非C++11版本的库文件.目前跑了几天,似乎是没出什么问题,但是我还是想说一下这样做有哪些潜在的风险. ...

  3. HTML&CSS基础-相对定位

    HTML&CSS基础-相对定位 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.HTML源代码 <!DOCTYPE html> <html> &l ...

  4. HTTP协议通信原理 与常见报错信息

    HTTP协议通信原理 请求报文 请求行 GET index.html HTTP 1.1 请求方法:get  读取服务器数据内容    post   提交存储服务端数据(用户注册) 协议版本:   ht ...

  5. 常见的linux上的服务重启脚本

    手写linux上的重启脚本,先把提纲列下 1.检查进程是否存在 存在杀死 2.备份原来的包到指定目录 3. 拉取新包,我这边为了简便,没有从jenkins slave上拿 4.启动命令 5.检查是否进 ...

  6. java项目中注解使用——整理

    文章:@Mapper注解的使用 地址:https://blog.csdn.net/weixin_39666581/article/details/81057385 @Mapper注解的的作用 1:为了 ...

  7. 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...

  8. FontLab

    FontLab 字体制作软件

  9. .NET Core SignalR 和 .NET SignalR 区别

    由于要转 .NET Core ,对于以前用到的一些进行迁移. 在迁移 SignalR 的时候发现 .NET Core 下的和 .NET 下的区别还是挺大的. 功能差异 自定重新连接 .NET 下的 S ...

  10. 一个s的力量——http与https

    我们网络上有很多资源,那么我们怎么来访问呢?就拿一个小例子来说,我们都会用百度来搜索东西,就会在地址栏中输入这样的地址http://www.baidu.com这种访问方式就是通过URL来实现的,那么什 ...