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 ...
随机推荐
- Winform 程序中dll程序集嵌入exe可执行文件
关于这方面,Google一下有很多方法,参考: http://blog.csdn.net/astonqa/article/details/7300856 但按照以上的方法我并没有成功,于是继续找到了一 ...
- hadoop之wordCount程序理解
有篇文章http://www.cnblogs.com/xia520pi/archive/2012/05/16/2504205.html中介绍的
- 使用Convert 类和Parse方法将字符串转换为数值类型
//用Parse方法将字符串转换为数值类型; long num=Int64.Parse(args[2]) //用别名为Int64c#类型long; long num=long.Parse(args[2 ...
- ASP.NET Excel数据导出数据库
/// <summary> /// 根據gridview導出excel /// </summary> /// <param name="ctl"> ...
- 收集 数据库的awr数据,生成报告
该脚本只是把awr报告的内容,原封不动的 展现出来,做记录 awrreport.sql 脚本内容如下: *********************************************** ...
- 找出整数中第k大的数
一 问题描述: 找出 m 个整数中第 k(0<k<m+1)大的整数. 二 举例: 假设有 12 个整数:data[1, 4, -1, -4, 9, 8, 0, 3, -8, 11, 2 ...
- grunt live
{ "name": "grunt-live-test", "version": "0.1.0", "autho ...
- JS 导出图片,toDataURL
//输出图片 function exportCanvasAsPNG(id, fileName) { //获取canvas元素 var canvasElement = document.getEleme ...
- nginx——location 优先级
一. location 的匹配符1.等于匹配符:=等于匹配符就是等号,特点可以概括为两点:精确匹配不支持正则表达式2.空匹配符空匹配符的特点是:匹配以指定模式开始的 URI不支持正则表达式3.正则匹配 ...
- YII 自动引入juquery进行表单验证
在form表单 里面引入这么一句话 array( 'enableClientValidation'=>true, 'clientOptions'=>array( ...