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 ...
随机推荐
- svn设置
svnserver -d -r /home/peter.mycode 如果想要开机自启动,将上述启动命令添加到:/etc/rc.local中.
- 部分A+B_1
正整数A的“DA(为1位整数)部分”定义为由A中所有DA组成的新整数PA.例如:给定A = 3862767,DA = 6,则A的“6部分”PA是66,因为A中有2个6. 现给定A.DA.B.DB,请编 ...
- 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 ...
- Codeforces 441D Valera and Swaps(置换群)
题意: 给定一个1~n的排列(n<=3000),输出字典序最小且次数最少的交换操作,使得操作后的排列可以通过最少m次交换得到排列[1,2,...n] Solution: 可以将排列的对应关系看做 ...
- 控制寄存器 CR*
控制寄存器(CR0-CR3)用于控制和确定处理器的操作模式以及当前执行任务的特性,如图4-3所示.CR0中含有控制处理器操作模式和状态的系统控制标志:CR1保留不用:CR2含有导致页错误的线性地址:C ...
- Java导出Excel和CSV(简单Demo)
Java导出Excel和CSV的简单实现,分别使用POI和JavaCSV. JavaBean public class ReportInfo { int id; String date; int nu ...
- 微博输入相关js 代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- php 详解spl_autoload_register()函数
在了解这个函数之前先来看另一个函数:__autoload. 一.__autoload 这是一个自动加载函数,在PHP5中,当我们实例化一个未定义的类时,就会触发此函数.看下面例子: printit.c ...
- windows7任务栏上的图标修复
Technorati 标记: 疑难杂症 今天,我在使用Windows 7的时候,因为操作一些系统文件,发现桌面下角的个别正在运行的图标不见了,但是,我们如果再打开一个新程序,又会提醒你已经在运行了 ...
- IIC 概述之1
概述: I²C 是Inter-Integrated Circuit的缩写,发音为"eye-squared cee" or "eye-two-cee" , 它是一 ...