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. 分布式session的几种实现方式

    在搭建完集群环境后,不得不考虑的一个问题就是用户访问产生的session如何处理.如果不做任何处理的话,用户将出现频繁登录的现象,比如集群中存在A.B两台服务器,用户在第一次访问网站时,Nginx通过 ...

  2. 用ffserver实现rtsp服务器的实验笔记

    参考:https://blog.csdn.net/hoyjam1/article/details/51281679 建议配置文件:/etc/config/ffserver.conf Port 1053 ...

  3. 常用实验报告LaTex 模板

    目录 模板1-无首页有表格头 模板2-有首页 模板1-无首页有表格头 % -*- coding: utf-8 -*- \documentclass{article} \usepackage{listi ...

  4. SqlHelper助手

    正在机房重构中,自己一直在摸索,刚开始听说SqlHelper只是感觉很高深,都不知道是用来做什么用的,只是看见别人的博客上写的可以用来帮助连接数据库.但自己没有什么特别的感觉,就认真的去查阅资料来具体 ...

  5. postgresql —— 零碎笔记

    聚合函数 -- 聚合查询 SELECT city, max(temp_lo) FROM weather WHERE city LIKE 'S%' GROUP BY city HAVING max(te ...

  6. Find Peak Element II

    Description Given an integer matrix A which has the following features : The numbers in adjacent pos ...

  7. MessageDigest的功能及用法

    MessageDigest 类为应用程序提供信息摘要算法的功能,如 MD5 或 SHA 算法.信息摘要是安全的单向哈希函数,它接收任意大小的数据,并输出固定长度的哈希值. MessageDigest ...

  8. Mysql开启慢查询命令

    ; 查询: SHOW VARIABLES LIKE '%slow_query_log%'; 结果:

  9. Jedis常用方法API

    一.键操作 二.字符串操作 三.整数和浮点数操作 四.列表(List)操作 五.集合(Set)操作 六.哈希(Hash)操作 七.有序集合(Zsort)操作 八.排序操作

  10. Mina学习+手写服务端+通过telnet连接服务端

    1. 2. 3. 4.MinaServer.java package com.mina; import java.io.IOException;import java.net.InetSocketAd ...