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. ASP.NET 打包下载文件

    使用的类库为:ICSharpCode.SharpZipLib.dll 一种是打包整个文件夹,另一种是打包指定的多个文件,大同小异: using ICSharpCode.SharpZipLib.Zip; ...

  2. [O] SQLite数据库报错:no such column

    在SQLite数据库创建语句增加列,运行后报错:no such column 在语法规范的前提下,即 //SQLite数据库创建,逗号与空格须严格 String CREATE_NOTE = " ...

  3. Premature optimization is the root of all evil.

    For all of we programmers,we should always remember that "Premature optimization is the root of ...

  4. Wireshark提示没有一个可以抓包的接口

    这是由于win下默认NPF服务是关闭的,需要以管理员的身份开启这个服务 Windows上安装wireshark时,会遇到NPF驱动无法启动的情况,一般如果采用管理员的方式就可以正常启动,或者是将NPF ...

  5. 十三、C# 事件

    1.多播委托 2.事件 3.自定义事件   在上一章中,所有委托都只支持单一回调. 然而,一个委托变量可以引用一系列委托,在这一系列委托中,每个委托都顺序指向一个后续的委托, 从而形成了一个委托链,或 ...

  6. yield用法的一点理解

    yield 关键字与 return 关键字结合使用,向枚举器对象提供值.这是一个返回值,例如,在 foreach 语句的每一次循环中返回的值.yield 关键字也可与 break 结合使用,表示迭代结 ...

  7. ROW_NUMBER分页的注意事项

    之前在使用ROW_NUMBER分页获取数据的时候,直接用ROW_NUMBER里的SELECT语句查出了所有的数据. like this: select * from ( select row_numb ...

  8. java.io.serializable

    为什么要实现 java.io.serializable? 简单点:“好处就是将来项目如果要做集群的话,就实现java.io.serializable接口”

  9. 搭建BCE本地开发环境

    1. 在官网下载VirtualBox & 虚拟机 http://bce.baidu.com/doc/BAE/GUIGettingStarted.html#.E4.B8.8B.E8.BD.BD. ...

  10. Egret 双端接入爱贝支付遇到的问题

    首先要为 egret 工程引入第三方库: Egret 接第三方库:http://edn.egret.com/cn/index.php?g=&m=article&a=index& ...