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 ...
随机推荐
- 10.4 noip模拟试题
题目名称 PA 青春 三部曲 名称 huakai taritari truetears 输入 huakai.in taritari.in truetears.in 输出 huakai.out tari ...
- updatepanel局部刷新功能,实现注册时对用户名的检测
updatepanel的使用 通过将控件放入到updatepanel中,实现局部刷新. 前台代码:<asp:ScriptManager ID="ScriptManager1" ...
- android SDK 代理配置(东北大学)
启动 Android SDK Manager ,打开主界面,依次选择「Tools」.「Options...」,弹出『Android SDK Manager - Settings』窗口: 在『Andro ...
- NSDate和NSString的转换及判定是昨天,今天,明天
用于uidate,picker.. +(NSDate*) convertDateFromString:(NSString*)uiDate{ NSDateFormatter *formatter ...
- C# 多线程、结构体
struct IpAndPort { public string Ip; public int Port; } private void Form1_Load(object sender, Event ...
- Elasticsearch学习2--Elasticsearch数据类型简介
1.Elasticsearch 是 面向文档型数据库,这意味着它存储的是整个对象或者 文档,它不但会存储它们,还会为他们建立索引,这样你就可以搜索他们了.你可以在 Elasticsearch 中索引. ...
- Java循环性能随笔
for iterator做迭代循环性能最好 然后是foreach 然后是提前声明好变量的for循环 最后是每次都要计算集合size的for package test; import j ...
- I/O多路转接之select
系统提供select函数来实现多路复⽤用输入/输出模型.select系统调用是用来让我们的程序监视 多个文件句柄的状态变化的.程序会停在select这里等待,直到被监视的文件句柄有一个或 多个发生了状 ...
- SGU 164.Airline(结论题)
时间限制:0.25s 空间限制:4M 题意: 在n(1<=n<=200)个点的无向完全图中,有m种不同的边.现在从中选择不超过(m+1)/2种边,使得任意两点可以通过不超过3条边互相到达. ...
- 调用数据库过程函数mysql
Connection conn=JdbcUtil.getConnection();//JdbcUtil是我写的获取connection的工具类 CallableStatement cast=conn. ...