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

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

Example 1:

Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]

Example 2:

Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1,-1]

用二分法分别查找最左位置 和最有位置。

class Solution {
public int[] searchRange(int[] nums, int target) {
int[] ret = new int[2];
ret[0] = binaryLeftSearch(nums,target,0,nums.length-1);
ret[1] = binaryRightSearch(nums,target,0,nums.length-1);
return ret;
} public int binaryLeftSearch(int[] nums, int target, int left, int right){
if(left > right) return -1; int mid = left + ((right-left)>>1);
int mostLeft;
if(target <= nums[mid]) {
mostLeft = binaryLeftSearch(nums,target,left,mid-1);
if(mostLeft == -1 && target==nums[mid]) mostLeft = mid;
}
else{ //target > nums[mid]
mostLeft = binaryLeftSearch(nums,target,mid+1,right);
}
return mostLeft;
} public int binaryRightSearch(int[] nums, int target, int left, int right){
if(left > right) return -1; int mid = left + ((right-left)>>1);
int mostRight;
if(target >= nums[mid]) {
mostRight = binaryRightSearch(nums,target,mid+1,right);
if(mostRight == -1 && target==nums[mid]) mostRight = mid;
}
else{ //target < nums[mid]
mostRight = binaryRightSearch(nums,target,left,mid-1);
}
return mostRight;
}
}

34. Find First and Last Position of Element in Sorted Array (JAVA)的更多相关文章

  1. Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)

    本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...

  2. 刷题34. Find First and Last Position of Element in Sorted Array

    一.题目说明 题目是34. Find First and Last Position of Element in Sorted Array,查找一个给定值的起止位置,时间复杂度要求是Olog(n).题 ...

  3. [LeetCode] 34. Find First and Last Position of Element in Sorted Array == [LintCode] 61. Search for a Range_Easy tag: Binary Search

    Description Given a sorted array of n integers, find the starting and ending position of a given tar ...

  4. [LeetCode] 34. Find First and Last Position of Element in Sorted Array 在有序数组中查找元素的第一个和最后一个位置

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

  5. (二分查找 拓展) leetcode 34. Find First and Last Position of Element in Sorted Array && lintcode 61. Search for a Range

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

  6. [leetcode]34.Find First and Last Position of Element in Sorted Array找区间

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

  7. leetcode [34] Find First and Last Position of Element in Sorted Array

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

  8. 【LeetCode】34. Find First and Last Position of Element in Sorted Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...

  9. 34. Find First and Last Position of Element in Sorted Array

    1. 原始题目 给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n) 级别. 如果数组中不存在 ...

随机推荐

  1. windows 全局安装 express 但无法命令行执行

    从 express 的官网直接按照命令行全局安装 express 后, express 命令无法执行, 在 cmd 中显示 该命令行无法识别.基本可判断是 环境变量配置 这边没有搞定. 1. 卸载 n ...

  2. hbuilderx 连接模拟器

    打开cmd,找到bin的安装目录(G:\Program Files\Nox\bin)       cd进入夜神模拟器bin目录 执行以下命令 nox_adb connect 127.0.0.1:620 ...

  3. JDK源码--HashMap(之resize)

    1.HashMap源码阅读目标了解具体的数据结构(hash及冲突链表.红黑树)和重要方法的具体实现(hashCode.equals.put.resize...) 2.重要方法 hashCode 与 e ...

  4. tihuantupian

  5. cat输出多行内容到文件

    输出格式是: cat > f1.sh <<end ...... end ----------------- == cat < f1.sh ## 同一行中的顺序可以改变 .... ...

  6. 目标双站定位仿真C++代码

    point-position2 初步完善版. 不再使用eigen库,行列式直接计算得出结果.判断共面异面分别处理. 先提取双站获得图像的匹配特征点,由双站位置信息解析目标位置. // point-po ...

  7. 将ubuntu系统录到u盘上

    可以使用bootice工具对u盘分区,并隐藏,然后把系统录到隐藏分区,ubuntu只需要1.5G即可.bootice很强大 录制工具可选的有Universal USB Install.UltraISO ...

  8. 中国MOOC_零基础学Java语言_第4周 循环控制_2念整数

    2 念整数(5分) 题目内容: 你的程序要读入一个整数,范围是[-100000,100000].然后,用汉语拼音将这个整数的每一位输出出来. 如输入1234,则输出: yi er san si 注意, ...

  9. 定制属于你自己的ViewEngine(一套逻辑多套UI)

    ASP.NET MVC出来这么久了,心中却又很多的疑惑:为什么所有的View都要放在Views目录下? 为什么Shared文件夹下面的页面可以被共享? 为什么Page既可以是*.cshtml,也可以是 ...

  10. element_to_be_clickable(locator)

    是等待页面元素可见的时候操作,会设置一定范围的时间,如果在时间范围内,元素可见,就 执行操作,元素不可见,就会引发TimeoutException的异常.如下是element_to_be_clicka ...