Search for a Range ——LeetCode
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的更多相关文章
- Search for a Range [LeetCode]
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- Search for a Range leetcode java
题目: Given a sorted array of integers, find the starting and ending position of a given target value. ...
- [LeetCode] 034. Search for a Range (Medium) (C++/Java)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
- [Leetcode][Python]34: Search for a Range
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...
- [array] leetcode - 34. Search for a Range - Medium
leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...
- 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 ...
- [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 ...
- 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 ...
- 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 ...
随机推荐
- Castle Windsor Fluent Registration API
一对一注册 直接注册组件 container.Register( Component.For<MyServiceImpl>() ); 注册接口并提供组件 container.Registe ...
- js substr和substring字符串截取
substr(start,length)第一个参数是开始位置(注:start的开始是从0开始,看到好多博客上面是从1开始,在火狐和谷歌执行了一下是从0开始),第二个参数是截取字符串的长度(可以省略,表 ...
- weChat聊天发送图片带有小尖角的实现
weChat聊天发送图片带有小尖角的实现 1.#import <UIKit/UIKit.h>2.3.@interface JKShapeImage : UIView4.5.@propert ...
- 完美让IE兼容input placeholder属性的jquery实现
调用时直接引用jquery与下面的js就行了,相对网上的大多数例子来说,这个是比较完美的方案. /* * 球到西山沟 * http://www.cnzj5u.com * 2014/11/26 12:1 ...
- css样式积累
1.圆角: border-radius:16px 16px 16px 16px; 2透明度: filter: alpha(opacity=80); ...
- Perl中级第四章课后习题
1.下列表达式各表示什么不同的含义: $ginger->[2][1] ${$ginger[2]}[1] $ginger->[2]->[1] ${$ginger->[2]}[1] ...
- OS X Yosemite下安装Hadoop2.5.1伪分布式环境
最近开始学习Hadoop,一直使用的是公司配好的环境.用了一段时间后发现对Hadoop还是一知半解,故决定动手在本机上安装一个供学习研究使用.正好自己用的是mac,所以没啥说的,直接安装. 总体流程 ...
- WF学习笔记(一)
-流程启动方式1: WorkflowInvoker.Invoke(new Workflow1()); -流程启动方式2: WorkflowApplication instance = new Work ...
- Java线程运行轨迹-代码追踪与定位
今天在写程序时,想到一个问题,当我的程序出异常的时候,控制台输出信息中,明确指出了出现异常的位置,并详细列举了函数的调用层次关系,它是怎么做到的. 竟然想到了这个问题,就去查看了源代码,不过没点几下, ...
- Unix/Linux 'dirctory tree' command.
ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/' It se ...