[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 ...
随机推荐
- asp.net web api集成微信服务(使用Senparc微信SDK)
/// <summary> /// 微信请求转发控制器 /// </summary> [RoutePrefix("weixin")] public clas ...
- ROS实时采集Android的图像和IMU数据
前言 临近毕业,整理一下之前做的东西.这篇博客来自于博主在2016年3月份投的一篇会议论文(论文主要介绍了一个基于手机摄像头和IMU的简单VIO系统,用于AR的Tracking部分,本博文 ...
- apache 指定的网络名不再可用 原因及解决方法
1.出现问题状况: 出现问题网站:http://www.ayyzz.cn/ 前段时间作文大全网出现有时候比较慢,有时候“找不到网页”404错误:另外在error.log里也报错: [Mon May 0 ...
- iOS.DistributionApp.0-build-adhoc-distribution-for-tester
Build adhoc distribution for tester 1. 提供App测试包 1.1 提供测试包的步骤 Ref[8] A: 注册所有的测试设备 B: 将App进行归档 C: 用ad ...
- JAVA期末设计第十三周
一.项目完成计划 十三周和十四周完成用户交互界面的设计(1.登陆界面2.订票以及查询界面3.用户管理界面4.退票界面): 十三周完成登陆界面,十四周完成订票以及查询界面,用户管理界面和退票界面 十五周 ...
- 【转载】SQL Server 2008 r2 中 SQL语句中单引号转义
sql server有两个转义符. 默认情况下, 单引号'是字符串的边界符, 如果在字符串中包含单引号', 则必须使用两个单引号', 第1个单引号'就是转义符.
- javascript面向对象(2)
主要内容: 作用域 在了解作用域之前,请先看一段代码: 通过运行示例可知,变量d和c报错.在预处理阶段,预处理会将全局中的判断语句忽略,直接加var声明的变量和function声明的函数. 作用域的分 ...
- shell <<EOF
1.考虑下面的需求,在主shell执行命令,进入其他的命令,后面的输入,想作为命令的输入,而不是主shell的输入,怎么办? 2.使用<<EOF,告诉主shell,后续的输入,是其他命令或 ...
- TCP/IP协议和HTTP协议 浩哥指教
TCP和IP在HTTP协议的上层,HTTP算是应用层,IP协议建立的是电脑跟电脑之间的联系,具体过程是,物理上,通过网线,解析MAC地址,到达路由,路由告诉数据将要去哪里,对方电脑通过NDS解析,解析 ...
- 蓝牙BLE实用教程
蓝牙BLE实用教程 Bluetooth BLE 欢迎使用 小书匠(xiaoshujiang)编辑器,您可以通过 设置 里的修改模板来改变新建文章的内容. 1.蓝牙BLE常见问答 Q: Smart Re ...