[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 ...
随机推荐
- mysql学习(3)-linux下mysql主从复制
前言:为什么MySQL要做主从复制(读写分离)?通俗来讲,如果对数据库的读和写都在同一个数据库服务器中操作,业务系统性能会降低.为了提升业务系统性能,优化用户体验,可以通过做主从复制(读写分离)来减轻 ...
- 【转】Win7 64bit Oracle 11g 使用PL/SQL Developer 连接时提示“SQL*Net not properly installed”
转载:http://www.cnblogs.com/xihuaodc/p/3189257.html 因为之前的Oracle不能用了,所以重新安装了64位的Oracle,安装一路正常 完了之后安装了P ...
- c# long转 datetime
; DateTime start = , , , , , , DateTimeKind.Utc); DateTime date = start.AddMilliseconds(time).ToLoca ...
- MAVEN解决Cannot change version of project facet Dynamic web module to 2.5
我们用Eclipse创建Maven结构的web项目的时候选择了Artifact Id为maven-artchetype-webapp,由于这个catalog比较老,用的servlet还是2.3的,而一 ...
- wcf http 代理
两个我都还没试,先记录着,其实我也不咋懂,所以记录着,权当一个线索 第一种 wcf中设置代理是在bing类中设置的,比如 WSHttpBinding ws = new WSHttpBinding(); ...
- 脉冲计数功能在ESM335x-Linux主板上的实现
1.综述 在工业控制中,经常需要获取脉冲信号计数值.频率.周期.占空比等参数.英创嵌入式主板ESM335X系列 Linux系统现已实现外部输入脉冲信号的计数.频率.周期.占空比测量功能. 主要功能及技 ...
- HttpWebRequest类
HttpWebRequest类与HttpRequest类的区别. HttpRequest类的对象用于服务器端,获取客户端传来的请求的信息,包括HTTP报文传送过来的所有信息.而HttpWebReque ...
- Linux下修改mysql密码
# /etc/init.d/mysql stop# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &# mysq ...
- 开启Python之路
开始自学Python 环境配置 自己百度去!!! 计算与变量 字符创.列表.元组和字典 简单的画图 使用if和else条件控制语句 循环 使用函数和模块来重用代码 使用类和对象 Python内建函数的 ...
- Jade之Plain Text
Plain Text jade提供了3种得到纯文本的方法. Piped Text 添加纯文本的一个最简单的方法就是在文本最前面加|符号即可. jade: p | It must always be o ...