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

题目大意:给定一个排序好的数组,找到指定数的起始范围,如果不存在返回[-1,-1];

解题思路:要求O(lgN)时间复杂度,二分查找。

  1. public int[] searchRange(int[] nums, int target) {
  2. int[] res = new int[2];
  3. if(nums==null||nums.length==0){
  4. return res;
  5. }
  6. res[0]=getLow(nums,target,0,nums.length-1);
  7. res[1]=getHigh(nums,target,0,nums.length-1);
  8. return res;
  9. }
  10. int getLow(int[] nums,int target,int low,int high){
  11. int mid=(low+high)>>1;
  12. if(low>high){
  13. return -1;
  14. }
  15. if(low==high){
  16. return nums[low]==target?low:-1;
  17. }
  18. if(nums[mid]==target){
  19. return getLow(nums,target,low,mid);
  20. }
  21. if(nums[mid]<target){
  22. low=mid+1;
  23. return getLow(nums,target,low,high);
  24. }else{
  25. high=mid-1;
  26. return getLow(nums,target,low,high);
  27. }
  28. }
  29. int getHigh(int[] nums,int target,int low,int high){
  30. int mid=(low+high)>>1;
  31. if(low>high){
  32. return -1;
  33. }
  34. if(low==high){
  35. return nums[low]==target?low:-1;
  36. }
  37. if(nums[mid]==target){
  38. int tmp=getHigh(nums,target,mid+1,high);
  39. int max=Math.max(tmp,mid);
  40. return max;
  41. }
  42. if(nums[mid]<target){
  43. low=mid+1;
  44. return getHigh(nums,target,low,high);
  45. }else{
  46. high=mid-1;
  47. return getHigh(nums,target,low,high);
  48. }
  49. }

Search for a Range ——LeetCode的更多相关文章

  1. Search for a Range [LeetCode]

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  2. Search for a Range leetcode java

    题目: Given a sorted array of integers, find the starting and ending position of a given target value. ...

  3. [LeetCode] 034. Search for a Range (Medium) (C++/Java)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...

  4. [Leetcode][Python]34: Search for a Range

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...

  5. [array] leetcode - 34. Search for a Range - Medium

    leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...

  6. LeetCode解题报告—— Search in Rotated Sorted Array & Search for a Range & Valid Sudoku

    1. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated(轮流,循环) at so ...

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

  8. LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  9. leetCode 34.Search for a Range (搜索范围) 解题思路和方法

    Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...

随机推荐

  1. svn设置

    svnserver -d -r /home/peter.mycode 如果想要开机自启动,将上述启动命令添加到:/etc/rc.local中.

  2. 部分A+B_1

    正整数A的“DA(为1位整数)部分”定义为由A中所有DA组成的新整数PA.例如:给定A = 3862767,DA = 6,则A的“6部分”PA是66,因为A中有2个6. 现给定A.DA.B.DB,请编 ...

  3. SGU 168.Matrix

    时间限制:0.5s 空间限制:15M 题意: 给出一个N*M的矩阵A,计算矩阵B,满足B[i][j]=min{ A[x][y]:(y>=j) and ( x>=i+j-y )} Solut ...

  4. Codeforces 441D Valera and Swaps(置换群)

    题意: 给定一个1~n的排列(n<=3000),输出字典序最小且次数最少的交换操作,使得操作后的排列可以通过最少m次交换得到排列[1,2,...n] Solution: 可以将排列的对应关系看做 ...

  5. 控制寄存器 CR*

    控制寄存器(CR0-CR3)用于控制和确定处理器的操作模式以及当前执行任务的特性,如图4-3所示.CR0中含有控制处理器操作模式和状态的系统控制标志:CR1保留不用:CR2含有导致页错误的线性地址:C ...

  6. Java导出Excel和CSV(简单Demo)

    Java导出Excel和CSV的简单实现,分别使用POI和JavaCSV. JavaBean public class ReportInfo { int id; String date; int nu ...

  7. 微博输入相关js 代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. php 详解spl_autoload_register()函数

    在了解这个函数之前先来看另一个函数:__autoload. 一.__autoload 这是一个自动加载函数,在PHP5中,当我们实例化一个未定义的类时,就会触发此函数.看下面例子: printit.c ...

  9. windows7任务栏上的图标修复

    Technorati 标记: 疑难杂症   今天,我在使用Windows 7的时候,因为操作一些系统文件,发现桌面下角的个别正在运行的图标不见了,但是,我们如果再打开一个新程序,又会提醒你已经在运行了 ...

  10. IIC 概述之1

    概述: I²C 是Inter-Integrated Circuit的缩写,发音为"eye-squared cee" or "eye-two-cee" , 它是一 ...