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. (57)Linux驱动开发之三Linux字符设备驱动

    1.一般情况下,对每一种设备驱动都会定义一个软件模块,这个工程模块包含.h和.c文件,前者定义该设备驱动的数据结构并声明外部函数,后者进行设备驱动的具体实现. 2.典型的无操作系统下的逻辑开发程序是: ...

  2. VS Code报错Module 'xx' has no 'xx' member pylint(no-member)解决办法

    pylint是vscode的python语法检查器,pylint是静态检查,在用第三方库的时候有些成员只有在运行代码的时候才会被建立,它就找不到成员,在设置(settings.json)里添加 &qu ...

  3. Spring Boot教程(二十一)开发Web应用(2)

    在完成配置之后,举一个简单的例子,在快速入门工程的基础上,举一个简单的示例来通过Thymeleaf渲染一个页面. @Controller public class HelloController { ...

  4. leetcode-mid-array-sorting and searching - 215 Kth Largest Element in an Array

    mycode  77.39% class Solution(object): def findKthLargest(self, nums, k): """ :type n ...

  5. Java8 新特性之集合操作Stream

    Java8 新特性之集合操作Stream Stream简介 Java 8引入了全新的Stream API.这里的Stream和I/O流不同,它更像具有Iterable的集合类,但行为和集合类又有所不同 ...

  6. 【C++ STL 优先队列priority_queue】

    https://www.cnblogs.com/fzuljz/p/6171963.html

  7. 在windows下使用Mingw搭建模拟Linux

    1.到官网下载最新版Mingw 2.点击安装,最好选择默认路径,如果不是的话,路径中一定不能有空格. 3.选择安装,mingw-developer-toolkit.mingw32-base.mingw ...

  8. JS获取select被选中的option的值

    一:JavaScript原生的方法 1:拿到select对象: var myselect=document.getElementById(“test”); 2:拿到选中项的索引:var index=m ...

  9. HTML CSS JS 特殊字符(转义)表

    HTML有许多特殊的字符,您对此有多少了解?平时在WEB制作中,您又有用到多少?或者说你在平时使用之时,是否也会碰到,有许多特殊字符要如何打印出来?比如说“笑脸”,比如说“版权号”.要是你用时忘记了这 ...

  10. python学习中

    python中的单引号.双引号.三引号的用法 网上也查找了资料,理解的都有些费劲 就自己验证了一下(主要是目前掌握的python知识,不知道什么时候会同时用到这三种引号) 用python3验证的 单引 ...