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. 大批量GPS坐标转百度坐标

    一. 百度地图API大批量转换时有数量限制,一个一个转.  用到的方法接口    /**      源坐标 格式:经度,纬度;经度,纬度… 最多支持100个;      源坐标类型:默认为1,即GPS ...

  2. android图片处理方法

    Java代码 //压缩图片大小 public static Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ...

  3. Python切片

    切片是啥, 可以吃么 切片肿么用哈 辣么长,记不住 切片是啥, 可以吃么 嘛,所谓切片故名思意就有选取的意思啦, 跟java里面的subString()意思差不多, 从原始的字符串中按规则提取出新的字 ...

  4. DDX_Text ()函数 C++

    DDX_Text()函数管理着对话框.表格视或控件视对象中的编辑控件与对话框.表格视或控件视对象的CString型数据成员之间的int,UINT,long,DWORD,CString,float或do ...

  5. window service 注册、启动、删除

    sc命令 sc actionName serviceName sc start serviceName sc stop serviceName sc delete serviceName : 删除注册 ...

  6. Shell--用户配置

    vim /etc/profileexport PS1='\[\e[1;33m\]\h\[\e[m\] \t [\[\e[1;36m\]\w\[\e[m\]] [\u] '   export LANG= ...

  7. Windows结构化异常

    不错的总结: http://hi.baidu.com/wangxvfeng101/item/518f6efdab4e5616ff35820e http://www.vckbase.com/index. ...

  8. (转)C语言16进制输出字符型变量问题

    最近在做一个C的嵌入式项目,发现在C语言中用printf()函数打印字符型变量时,如果想采用"%x"的格式将字符型变量值以十六进制形式打印出来,会出现一个小问题,如下: char  ...

  9. 判断是否是有效的IPV4地址

    参考链接: https://blogs.msdn.microsoft.com/oldnewthing/20060522-08/?p=31113 http://www.cnblogs.com/txw19 ...

  10. 解决EasyUI动态添加标签渲染问题

    以下代码用于Js脚本中: var Work_Content_Back = "<table width='99%' class='table' style='margin-bottom: ...