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. HDU 4749-Parade Show(KMP变形)

    题意: 给出一个母串和一个模式串求母串中最多能分成最大的子串数(每个字串中的各个数字的大小关系和模式串相同) 分析: KMP变形匹配规则变一下即可,用当前数字和下个数字的差表示,大小关系方便匹配 #i ...

  2. Qt使用Cookies对网站操作之Get和POST

    1.添加QNetwork模块: a.Qt Creator中打开.pro文件添加QT+=Network; b.VS_Qt中项目属性中Qt Project Settings中Qmodules中勾选”QNe ...

  3. openfl关于windows平台编译报错解决办法

    报错信息:  无法打开程序数据库“e:\newproj\mainclient\bin\windows\cpp\obj\obj\msvc-debug-ncxp\vc.pdb”:如果要将多个 CL.EXE ...

  4. 【Spark学习】Apache Spark作业调度机制

    Spark版本:1.1.1 本文系从官方文档翻译而来,转载请尊重译者的工作,注明以下链接: http://www.cnblogs.com/zhangningbo/p/4135905.html 目录 概 ...

  5. CentOS上firefox安装flash

    CentOS下firefox安装flash说明 CentOS下自带了firefox,但没有flash插件的,按它自己的提示安装不成功,需要手动安装,如下: 1.打开flash官网,http://lab ...

  6. 实现 Web 后端和客户端之间的分布式和认证通讯

    stack.io 是一个用于实现 Web 后端和客户端之间的分布式和认证通讯. 服务器端进程之间的通讯是非常高效的,因为没有中间的代理.而来自客户端的请求通过 socket.io 进入 Node.js ...

  7. Windows下GNU之gcc体验方法

    Windows 现在在Windows下开发C/C++程序一般都是用微软的编译器,当年的Borland已经成为传说.但是如果你不想付钱的话,也可以考虑Windows下的GCC. 在Windows下体验G ...

  8. 在Windows Server 下安装 Oracle 11G 的一般步骤

  9. 转】Apache解决高并发和高可用

    原博主于: http://www.ha97.com/5803.html   感谢! 服务器集群 Apache 和 nginx(web服务器) 1.  多台集群机器联合处理一个任务. 2.  一台机器处 ...

  10. JavaScript——以简单的方式理解闭包

    闭包,在一开始接触JavaScript的时候就听说过.首先明确一点,它理解起来确实不复杂,而且它也非常好用.那我们去理解闭包之前,要有什么基础呢?我个人认为最重要的便是作用域(lexical scop ...