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)时间复杂度,二分查找。

    public int[] searchRange(int[] nums, int target) {
int[] res = new int[2];
if(nums==null||nums.length==0){
return res;
}
res[0]=getLow(nums,target,0,nums.length-1);
res[1]=getHigh(nums,target,0,nums.length-1);
return res;
}
int getLow(int[] nums,int target,int low,int high){
int mid=(low+high)>>1;
if(low>high){
return -1;
}
if(low==high){
return nums[low]==target?low:-1;
}
if(nums[mid]==target){
return getLow(nums,target,low,mid);
}
if(nums[mid]<target){
low=mid+1;
return getLow(nums,target,low,high);
}else{
high=mid-1;
return getLow(nums,target,low,high);
}
}
int getHigh(int[] nums,int target,int low,int high){
int mid=(low+high)>>1;
if(low>high){
return -1;
}
if(low==high){
return nums[low]==target?low:-1;
}
if(nums[mid]==target){
int tmp=getHigh(nums,target,mid+1,high);
int max=Math.max(tmp,mid);
return max;
}
if(nums[mid]<target){
low=mid+1;
return getHigh(nums,target,low,high);
}else{
high=mid-1;
return getHigh(nums,target,low,high);
}
}

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. setTimeout 方法用于在指定的毫秒数后调用函数或计算表达式

    setTimeout 方法用于在指定的毫秒数后调用函数或计算表达式

  2. 关于ADO.NET的一些知识整理

    ADO.NET是什么 虽然我们都知道ADO.NET是对数据库的操作,但是要真的说出ADO.NET的具体含义还不是很容易. ADO.NET是ActiveX Data Objects的缩写,它是一个COM ...

  3. PHP 开启报错机制

    屏蔽PHP错误提示 方法一:在有可能出错的函数前加@,然后or die("") 如: @mysql_connect(...) or die("Database Conne ...

  4. webGIS(离线版)研究路线归总

    特注:不做详解,说明网上资源很多,找一篇,照着走一遍即可. 1.数据源要满足开源.Free且地理信息要完善 几经周折,选择了OSM,具体信息可以去其官方查看(它竟然把中国一分为二,大陆.台湾,坑爹!! ...

  5. ftp不能上传解决办法

    自己的服务器,然后我分好ftp,有一天,有个客户要传东西,发现怎么也传不上,但是可以下载,,,,弄了半天,目录权限也是完全访问,但还是不行,原来是serv-u分配的空间小了啊,所以只能下载不能上传.. ...

  6. windows sever 2008 r2 - 限制ip访问

    和win 7 旗舰版不同,该操作系统在安装IIS后,非本机的并不能直接访问主机.需要设置主机上的本机的IIS中的IP地址和域限制. 由于我是想在同一个局域网(路由器)中,通过Android操作系统访问 ...

  7. sql 作业+游标 自动备份数据库

    前言 昨天有个同事在客户的服务器上面弄数据库,不小心执行了一条 sql 语句 TRUNCATE TABLE xxx 碉堡了吧,数据全没了  - - ,然后就是在网上拼命的搜索关于数据恢复的软件,搞了一 ...

  8. oracle 触发器number判断空值,:NEW赋值,for each row,sql变量引号,to_date,to_char

    1.number类型在库中可能存在null这种数据 判断是否为空时要用如下: IF(nvl(:NEW.BACAH,0) <>0) 不能用IF(BACAH IS NOT NULL) 2. 2 ...

  9. Java线程(学习整理)--3--简单的死锁例子

    1.线程死锁的概念: 简单地理解下吧! 我们都知道,线程在执行的过程中是占着CPU的资源的,当多个线程都需要一个被锁住的条件才能结束的时候,死锁就产生了! 还有一个经典的死锁现象: 经典的“哲学家就餐 ...

  10. hdoj 2191(多重背包)

    悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/ ...