【LeetCode OJ 34】Search for a Range
题目链接:https://leetcode.com/problems/search-for-a-range/
题目: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].
解题思路:题意为在一个有序数组中寻找指定值的起始位置和结束位置,由于题目要求时间复杂度为O(log n)。故而想到使用二分查找法。
演示样例代码:
public class Solution
{
public int[] searchRange(int[] nums, int target)
{
int[] result=new int[]{-1,-1};
int start=0;
int end=nums.length-1;
while(start<=end)
{
int middle=(start+end)>>1;
int middleValue=nums[middle];
if(middleValue==target)
{
//找到目标值后,先搜索其左边有没有与目标值相等的数,取其最左边的索引值放入result中。同理再搜索其最右边
int i=middle;
while((i-1)>=0&&nums[i-1]==target)
{
i--;
}
result[0]=i;
int j=middle;
while((j+1)<nums.length&&nums[j+1]==target)
{
j++;
}
result[1]=j;
return result;
}
else if(target<middleValue)
{
//小于中值时在中值前面找
end=middle-1;
}
else
{
//大于中值在中值后面找
start=middle+1;
} }
return result;
}
}
【LeetCode OJ 34】Search for a Range的更多相关文章
- 【LeetCode OJ 016】3Sum Closest
题目链接:https://leetcode.com/problems/3sum-closest/ 题目:Given an array S of n integers, find three integ ...
- 【LeetCode OJ 232】Implement Queue using Stacks
题目链接:https://leetcode.com/problems/implement-queue-using-stacks/ 题目:Implement the following operatio ...
- 【LeetCode OJ 136】Single Number
题目链接:https://leetcode.com/problems/single-number/ 题目:Given an array of integers, every element appea ...
- 【LeetCode OJ 268】Missing Number
题目链接:https://leetcode.com/problems/missing-number/ 题目:Given an array containing n distinct numbers t ...
- 【LeetCode OJ 14】Longest Common Prefix
题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest co ...
- LeetCode(34)Search for a Range
题目 Given a sorted array of integers, find the starting and ending position of a given target value. ...
- 【leetcode 字符串处理】Compare Version Numbers
[leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...
- 【LeetCode算法-27】Remove Element
LeetCode第27题 Given an array nums and a value val, remove all instances of that value in-place and re ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
随机推荐
- 模块打包机--webpack--基础使用
什么是webpack? 作用有哪些? WebPack可以看做是模块打包机:它做的事情是,分析你的项目结构,找到JavaScript模块以及其它的一些浏览器不能直接运行的拓展语言(Scss,Type ...
- nignx 502错误不能使用/的路径方式 即pathinfo
在server中加入 include enable-php-pathinfo.conf; 引入nginx.conf下的这个文件即可. 如果是tp框架,主要隐藏index.php的入口文件,再加入下面这 ...
- jquery weui ajax滚动加载更多
手机端使用jquery weui制作ajax滚动加载更多. 演示地址:http://wx.cnkfk.com/nuol/static/fpage.html 代码: <!DOCTYPE html& ...
- Maven多模块项目搭建
最近一直在思考如何能够更好的重用代码.减少重复劳动,之前有一篇文章通过导入JAR包的形式,可以重用部分形如util类的方法,但是这样的话,管理起来jar包,特别是协同工作,多项目情况下,管理JAR会出 ...
- ArcGIS Server 10.2 公布Oracle11g数据源的 Feature Service
安装好arcgis server 10.2及 Desktop 而且确保 arcgis server manager 能够正常启动执行载入服务 1.Oracle 配置 安装好Oracleserver端程 ...
- WebRTC代码走读(八):代码文件夹结构
转载注明出处http://blog.csdn.net/wanghorse ├── ./base //基础平台库,包含线程.锁.socket等 ├── ./build //编译脚本.gyp ├── ./ ...
- quartz.net定时任务框架详解
C#做定时任务:一是Timer:而是Quartz.net:本文就学习一下Quartz.net框架 Quartz.net非常的灵活,开发人员能用很少的代码就能完成“每隔一小时执行”.“每天2点执行”.“ ...
- 解决局域网内无法IP访问IIS已发布的网站
在IIS上发布的网站,本地可以访问,但是局域网内其他电脑却访问不了,原来是防火墙的问题,关闭它就可以访问了. 上面是我的简单操作 后来又百度了一下,发现有个更详细的操作:http://jingyan. ...
- kinEditor动态渲染的问题
摘自:jingyan.baidu.com/article/a65957f4a4c89a24e67f9b3d.html 在使用kindEditor时,因为textarea是动态加载的,因而对textar ...
- eclipse01
http://blog.csdn.net/luman1991/article/category/6354903