import java.util.Arrays;

/**
* Source : https://oj.leetcode.com/problems/search-for-a-range/
*
* Created by lverpeng on 2017/7/14.
*
* 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].
*
*/
public class SearchRange { /**
* 查找target在有有序数组中的起始位置
*
* 先找左边界,普通二分查找是和target比较,如果相同就返回,这里小于等于num[mid],如果是等于num[mid]也是收缩右边,最后得到的就是左边界
* 右边界同上
*
* @param num
* @param target
* @return
*/
public int[] search (int[] num, int target) {
int left = 0;
int right = num.length - 1;
int mid = 0;
while (left <= right) {
mid = (left + right) / 2;
if (num[mid] >= target) {
right = mid - 1;
} else {
left = mid + 1;
}
} int targetLeft = left;
left = 0;
right = num.length - 1;
while (left <= right) {
mid = (left + right) / 2;
if (num[mid] <= target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
int targetRight = right;
if (target != num[targetLeft] || target != num[targetRight]) {
targetLeft = targetRight = -1;
}
int[] result = new int[2];
result[0] = targetLeft;
result[1] = targetRight;
return result;
} public static void main(String[] args) {
SearchRange searchRange = new SearchRange();
int[] arr = new int[]{5, 7, 7, 8, 8, 10};
System.out.println(Arrays.toString(searchRange.search(arr, 8)));
} }

leetcode — search-for-a-range的更多相关文章

  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 (TODO)

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

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

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

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

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

随机推荐

  1. WebAPI之DOM和BOM

    API是什么? Application Programming Interface:应用程序编程接口,是一些预先定义的函数,通俗的理解就是一些方法. WebAPI是什么? 浏览器提供的一套操作浏览器功 ...

  2. 制作DNS字典

    1.收集字典 一般kali自带的DNS爆破工具都会有自己的字典,使用  dpkg -L dns爆破软件名 查询字典的路径.txt文件一般是字典. 合并到一个txt文件中. 2.删除字典中重复的字符串 ...

  3. Javascript中表达式和语句的区别

    一.表达式:一个表达式会产生一个值,它可以放在任何需要一个值的地方,比如,作为一个函数调用的参数. 以下例子就是表达式: a=35: b=1+a; a=function (){return 6}: b ...

  4. Linux环境下Redis集群实践

    环境:centos 7 一.编译及安装redis源码 源码地址:redis版本发布列表 cd redis-3.2.8 sudo make && make install 二.创建节点 ...

  5. explicit_defaults_for_timestamp引发的狗血剧情

    今天就碰到了一个较初级的问题,居然为找这个参数花了好半天时间,深以为不齿.需求是这样的,有个表的某个字段需要从datetime改成timestamp类型.原结构如下:create table tmp1 ...

  6. Adnroid开发环境搭建(四步搞定)

    新手博友,多多关照 下面给大家介绍JDK Eclipse AndroidSDK ADT环境搭建,安装教程 第一步.安装JDK: 第二步.安装Eclipse: 第三步.下载并安装AndroidSDK: ...

  7. one-to-one 一对一映射关系(转 wq群)

    2.配置对应的xml配置文件 person的配置文件 idCard的配置文件 idCard的配置文件  3.测试 运行测试程序后,控制台输出两条语句

  8. Django积木块七——视频

    视频 # 在网上搜索video.js然后下载相关的js和css文件,看文档正确使用视频模块,添加视频外链 <div style="width: 1200px;height: 675px ...

  9. GC算法基础

    寻找垃圾对象的算法:1. 引用计数(无法处理循环引用) 2. 根寻法(被广泛引用在gc算法中) 清理垃圾的算法: 1. 标记复制  2. 标记清理  3. 标记整理 分代算法的好处: 1. 分代处理, ...

  10. WordPress自动裁剪768w像素缩略图的解决办法

    最新观赏鱼在折腾一个新的WordPress站点,即使通过后台把多媒体裁剪的宽高都设置为0时,移除主题可能存在的自动裁剪大小,WordPress依然会在上传图片的时候自动裁剪一个宽为768像素的图片.并 ...