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的更多相关文章

  1. [array] leetcode - 34. Search for a Range - Medium

    leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...

  2. [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 ...

  3. 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 ...

  4. 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 ...

  5. LeetCode 34. Search for a Range (找到一个范围)

    Given an array of integers sorted in ascending order, find the starting and ending position of a giv ...

  6. 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 ...

  7. LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)

    题目链接: https://leetcode.com/problems/search-for-a-range/?tab=Description   Problem: 在已知递减排序的数组中,查找到给定 ...

  8. Java [leetcode 34]Search for a Range

    题目描述: Given a sorted array of integers, find the starting and ending position of a given target valu ...

  9. [Leetcode][Python]34: Search for a Range

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...

随机推荐

  1. WCF use ProtoBuf

    ProtoBuf, 比起xml和json, 传输的数据里面没有自描述标签, 而且是基于二进制的, 所以有着超高的传输效率, 据牛人张善友的描述, 可以替代WCF的自带的编码方案, 效率有极大的提升. ...

  2. 安装软件 报错1304 C:\Program Files\Common Files\VMware\VMware VMRC Plug-in\Firefox\dbghelp.dll验证您对该目录具有访问权限

    现在情况是C:\Program Files\Common Files\VMware\VMware VMRC Plug-in\Firefox这个文件夹里面没有dbghelp.dll文件,就需要在百度上面 ...

  3. [LeetCode][Java]Triangle@LeetCode

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  4. Matlab(3) -- 编写M文件(函数)

    转自:http://blog.csdn.net/misskissc/article/details/8178089 matlab的命令编辑窗口(Command Window)界面主要是用来调用系统命令 ...

  5. javascript面向对象(三)

    主要内容: 利用原型链的方式实现继承: 原型继承的特点:即继承了父类的模板,也继承了父类的原型对象. 类继承:只继承模板(借用构造函数的方式继承). 利用call.apply方法实现: 混合继承: 扩 ...

  6. 第四章 使用Docker镜像和仓库

    第4章 使用Docker镜像和仓库 回顾: 回顾如何使用 docker run 创建最基本的容器 $sudo docker run -i -t --name another_container_mum ...

  7. html中 table 和 form的位置

    对于web前端开发来说  经常会用到 像firebug这样审查元素 工具 发现了一个这样的现象: 当 able><form><tr>....</tr>< ...

  8. 如何去掉div滚动条

    1.去掉横向滚动条 style="overflow-x:hidden" 2.去掉纵向滚动条 style="overflow-y:hidden" 3.同时去掉横向 ...

  9. button、label、textfield、页面跳转、传值

    .AppDelegate.m #import “OneViewController.h” //一打开就运行的 -(BOOL)application:(UIApplication *)applicati ...

  10. 轮播神器swiper插件

    Swiper中文网:http://www.swiper.com.cn/ Swiper- 是免费的,最现代化的移动触摸滑块硬件加速的转换和惊人的天然行为.它的目的是在移动网站,移动网络应用和移动本地/混 ...