[leetcode 34] search for a range
1 题目:
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].
2 思路:
好吧,刚开始看到这个题目,一想,不就是一个二分查找吗。然后再两边找相等的。
结果提交呵呵了,运行时间只超过0.5%的人。
看别人的代码,原来要两次二分搜索,一次找左边位置,一次要找右边位置。
看懂了其中一个人的代码,主要是 searchFirstEqualOrGreater这个函数。调用两次就确定范围了。
3 代码:
public class Solution {
public int[] searchRange(int[] nums, int target) {
if(nums == null || nums.length == ){
return new int[] {-,-};
}
int[] result = new int[];
result[] = findFirstEqualOrGreater(nums,target);
if(result[] == nums.length || nums[result[]] != target){
return new int[] {-,-};
}
result[] = findFirstEqualOrGreater(nums,target+)-;
return result;
}
private int findFirstEqualOrGreater(int[] nums,double target){
int lo = ;
int hi = nums.length;
int index = -;
while(lo < hi){
index = lo + ((hi-lo)>>);
if (nums[index]<target){
lo = index+;
}else{
hi = index;
}
}
return lo;
}
}
[leetcode 34] search for a range的更多相关文章
- [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] 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 34.Search for a Range (搜索范围) 解题思路和方法
Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...
- 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 ...
- LeetCode 34. Search for a Range (找到一个范围)
Given an array of integers sorted in ascending order, find the starting and ending position of a giv ...
- leetcode@ [34] Search for a Range (STL Binary Search)
https://leetcode.com/problems/search-for-a-range/ Given a sorted array of integers, find the startin ...
- LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)
题目链接: https://leetcode.com/problems/search-for-a-range/?tab=Description Problem: 在已知递减排序的数组中,查找到给定 ...
- Java [leetcode 34]Search for a Range
题目描述: Given a sorted array of integers, find the starting and ending position of a given target valu ...
- [Leetcode][Python]34: Search for a Range
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...
随机推荐
- 判断当前VC是不是正在展示的活跃页面
viewController.isViewLoaded && viewController.view.window
- 【随笔】Linux服务器备份相关
服务器数据的安全性一直都是服务器日常管理的重中之重.Linux服务器虚拟化虽然以其高度可靠的作业系统而闻名,不过系统失效仍然可能发生.可能因为硬体故障,电源中断,或其他不可预料的问题.更常见的这 些问 ...
- gulp插件autoprefixer
gulp的autoprefixer插件可以根据我们的设置帮助我们自动补全浏览器的前缀(如:-moz.-ms.-webkit.-o) 1)首先安装gulp,不知道怎么安装请看这里 2)安装autopre ...
- bc:linux下命令行计算器
在linux下,存在一个命令行的计算器:bc.该程序一般随发行版发布. bc计算器能够执行一些基本的计算,包括+,-,×,\,%. 这些计算不经针对十进制,还可以使用二进制,八进制,十六进制,并且可以 ...
- 分享自制的13套 JQuery Mobile 界面主题(追加4套新款)
15套整合在一起的,其中2套官方+13套自制,款款精致,方便移动开发. 字体默认为微软雅黑. 适配于 JQuery Mobile 1.4.3 下载地址:http://files.cnblogs.com ...
- 一个无缝滚动的jquery插件
$.fn.imgscroll = function(o){ var defaults = { speed: 40, amount: 0, width: 1, dir: "left" ...
- 编译安装或者mysql启动时遇到的错误小记
编译安装遇到的错误:进入mysql目录 [root@localhost software]# cd mysql-5.6.19 [root@localhost mysql-5.5.11]# cmake ...
- Linux下高cpu占有率的调试方案
1.用top命令查看哪个进程占用CPU高 gateway网关进程14094占用CPU高达891%,这个数值是进程内各个线程占用CPU的累加值. 2.用top -H -p pid命令查看进程内各个线 ...
- IIS 7 Web服务器上部署ASP.NET网站(转)
IIS 7 Web服务器上部署ASP.NET网站小记 摘自:http://swanmsg.blog.sohu.com/162111073.html 网上查找了很久关于iis7配置asp.net配置问题 ...
- 《CODE》读后笔记——第1~13章
1.电筒密谈 Morse code表 文中提到"英语词汇就是一种编码".这句话仿佛有一种哲学思想在里面,万物皆可以以任何形式编码,只是编码的方式和途径不同.有些编码简单易懂易于接受 ...