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操作系统Apache服务器下配置PHP

    在Apache web服务器上发布PHP项目之前,需要进行相应的配置,服务器才能解析php文本,正常显示php动态页面内容.在进行php配置之前默认已经在Windows系统下安装好了Apache服务器 ...

  2. nmon性能监控

    1.nmon下载地址 2../nmon_x86_rhel52 3.根据上面提示的快捷键进行输入即可显示相应的资源耗用情况,如输入:c.m.d(显示cpu.内存.磁盘使用情况) 4.输入数据到文件 ./ ...

  3. android系统时间格式转换工具类

    代码依旧非常简单,只不过因为这个方法极为常用,因此体现的还是封装的思想. package com.ctbri.weather.utils; import java.text.SimpleDateFor ...

  4. (转)搭建自己的Nuget服务器

    转:https://www.cnblogs.com/knowledgesea/p/5500954.html 序言 你们公司有没有好多项目,有没有好多类库,你们的类库是在tfs中管理,还是svn或者gi ...

  5. MySQL主从服务器的原理和设置

    一 主从配置的原理     mysql的Replication是一个异步的复制过程,从一个mysql instance(Master)复制到另一个mysql instance(Slave), 在mas ...

  6. 案例ORA-00600: internal error code, arguments: [qkaffsindex3], [], [], [], []

    执行更新统计信息语句: exec dbms_stats.gather_schema_stats(ownname=>'LIVE_KS',degree=>2,cascade=>true, ...

  7. java基础--单例模式的7种实现【转载】

    转载:http://www.blogjava.net/kenzhh/archive/2013/03/15/357824.html 第一种,线程不安全(懒汉模式) 1 public class Sing ...

  8. 阶段3 1.Mybatis_09.Mybatis的多表操作_9 mybatis多对多操作-查询用户获取用户所包含的角色信息

    sql语句以user作为主表 用户的全部信息,以为用户下的角色的.并不是所有的用户都有角色,有角色的就带角色.没角色的就为null 首先修改实体类 定义List<Role> 生成gette ...

  9. RESR API (三)之Views

    Class-based Views Django's class-based views are a welcome departure from the old-style views. - Rei ...

  10. spring-mvc 3.* 多视图解析配置实例 ContentNegotiatingViewResolver

    一.起因     从spring 3.1.0升级到spring 3.2.0时,配置文件servlet.xml中出错. 错误信息: java.lang.String cannot be cast to  ...