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

思路:

使用二分搜索找到target的idx,然后查看该idx的左右确定范围。

算法复杂度:

平均情况下是O(lgn);

最坏情况下数组中所有元素都相同O(n);

 public class Solution {
public int[] searchRange(int[] A, int target) {
// Start typing your Java solution below
// DO NOT write main() function
int idx = binarySearch(A, target);
int len = A.length;
int[] results = null;
if(idx == -1){
results = new int[]{-1, -1};
} else{
int l = idx;
int r = idx;
while(l >= 0 && A[l] == target){
l--;
}
l++; while(r < len && A[r] == target){
r++;
}
r--;
results = new int[]{l, r};
}
return results;
} public int binarySearch(int[] A, int target){
int len = A.length;
int l = 0, r = len - 1;
while(l <= r){
int mid = (l + r) / 2;
if(target == A[mid])
return mid; if(target > A[mid]){
l = mid + 1;
} else {
r = mid - 1;
}
} return -1;
}
}

google了下,要保证最坏情况下时间复杂度为O(lgn):进行两次二分搜索确定左右边界

leetcode -- Search for a Range (TODO)的更多相关文章

  1. LeetCode: Search for a Range 解题报告

    Search for a RangeGiven a sorted array of integers, find the starting and ending position of a given ...

  2. [LeetCode] Search for a Range 搜索一个范围

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  3. [LeetCode] Search for a Range(二分法)

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  4. leetcode Search for a Range python

    class Solution(object): def searchRange(self, nums, target): """ :type nums: List[int ...

  5. [LeetCode] Search for a Range 二分搜索

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  6. Leetcode Search for a Range

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  7. leetcode:Search for a Range(数组,二分查找)

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  8. [LeetCode] Search for a Range [34]

    题目 Given a sorted array of integers, find the starting and ending position of a given target value. ...

  9. LeetCode Search for a Range (二分查找)

    题意 Given a sorted array of integers, find the starting and ending position of a given target value. ...

随机推荐

  1. Number Sequence(KMP,判断子串 模板)

    题意: 给两数组,求一个是否是另一个的子数组,若是返回匹配的首位置 分析: KMP 入门 //扫描字符串A,并更新可以匹配到B的什么位置. #include <map> #include ...

  2. codeforces 682D Alyona and Strings

    #include <cstdio> #include <iostream> #include <ctime> #include <vector> #in ...

  3. <译>Selenium Python Bindings 2 - Getting Started

    Simple Usage如果你已经安装了Selenium Python,你可以通过Python这样使用: #coding=gbk ''' Created on 2014年5月6日 @author: u ...

  4. Tableau学习笔记之五

    计算用户自定义字段,虽然在Tableau软件中已经加入了很多的数值操作运算,比如平均值,最大值等,但是可以自定义自己需要的数值操作运算. 数值操作可以有以下:预定义函数,百分比,总计,分级等等 1.直 ...

  5. 关于duilib中的list的扩展探索

    原文地址:http://blog.csdn.net/tragicguy/article/details/21893065 今天在做一个程序的界面时,需要在一个列表中显示除文字以外的其他控件,如:Edi ...

  6. 【ActiveX】实现安全接口

    转自:http://www.cnblogs.com/carekee/articles/1772201.html 感谢原作者! ActiveX控件打包成cab后,在脚本中调用中时,要保证控件的安全性才能 ...

  7. MDI端口和MDIX端口是什么? 又有什么作用?

    是网线的标准A类接法和B类接法.也就是人们通常所说的交叉网线和直联网线.直联网线就是 白黄 黄 白绿 蓝 白兰 绿 白棕 棕 另一端同样如此.交叉网线就是 另一端的1和3,2和6对调.这样就成了交叉网 ...

  8. python学习之dict的items(),values(),keys()

    Python的字典的items(), keys(), values()都返回一个list >>> dict = { 1 : 2, 'a' : 'b', 'hello' : 'worl ...

  9. bzoj 3997 [TJOI2015]组合数学(DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3997 [题意] 给定一个nm的长方形,每次只能使经过格子权值减1,每次只能向右向下,问 ...

  10. MySQL CURDATE() 函数

    定义和用法 CURDATE() 函数返回当前的日期. 语法 CURDATE() 实例 例子 1 下面是 SELECT 语句: SELECT NOW(),CURDATE(),CURTIME() 结果类似 ...