Given an array of integers nums sorted in ascending order, 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].

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) {
// corner case
if(nums == null || nums.length == 0) return new int[]{-1,-1};
int left = findFirst(nums, 0, nums.length-1, target);
if(left == -1) return new int[]{-1,-1};
int right = findLast(nums, 0, nums.length-1, target);
return new int[]{left, right}; }
// find start point
private int findFirst(int[] nums, int left, int right, int target) {
// avoid dead looping
while(left + 1 < right){
// avoid overflow
int mid = left + (right - left)/2;
// left------ |mid| ---target---right
if(nums[mid] < target){
left = mid;
}
// left---target---|mid| ------right
else{
right = mid;
}
}
if (nums[left] == target) return left;
if (nums[right] == target) return right;
return -1;
} private int findLast(int[] nums, int left, int right, int target) {
while(left + 1 < right){
int mid = left + (right - left)/2;
// left---target---|mid| ------right
if(nums[mid] > target){
right = mid;
}
// left------ |mid| ---target---right
else{
left = mid;
}
}
if(nums[right] == target) return right;
if (nums[left] == target) return left;
return -1;
}
}

[leetcode]34.Find First and Last Position of Element in Sorted Array找区间的更多相关文章

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

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

  2. [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 ...

  3. [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 ...

  4. (二分查找 拓展) 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 ...

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

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

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

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

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

  8. leetcode个人题解——#34 Find First and Last Position of Element in Sorted Array

    思路:先二分查找到一个和target相同的元素,然后再左边二分查找左边界,右边二分查找有边界. class Solution { public: , end = -; int ends; int lS ...

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

    题意懒得抄了,大概是:在升序数组中给定整数target,找到第一个和最后一个target的索引,找到返回{index1, index2},否则返回{-1, -1}: 时间复杂度要求:O(logn) 分 ...

随机推荐

  1. python 的 format 函数

    python的格式化字符串方法之一------------format 函数 它通过{}和:来代替%. 数字 格式 输出 描述 3.1415926 {:.2f} 3.14 保留小数点后两位 3.141 ...

  2. 整数的故事(4)——Karastuba算法

    我们在小学就学过用竖式计算两个多位数的乘法: 这个过程简单而繁琐,没有最强大脑的普通大众通常是用计算器代替的.然而对于超大整数的乘法,计算器也未必靠得住,它还存在“溢出”一说.这就需要我们自行编写算法 ...

  3. 如何在idea中引入一个新maven项目

    如何在idea中引入一个新的maven项目,请参见如下操作:      

  4. mysql插中文出现错误 "incorrect string value:\x.....

    mysql字符集的问题: mysql的表格整理应该改为gbk_chinese_ci,每个需要汉字的字段属性的整理也改为gbk_chinese_ci,

  5. 如何在Windows命令行(DOS界面)中调用 编译器 来编译C/C++源程序

    首先说明一下背景: 为什么要在DOS界面编译C/C++源程序?有很多现成的开发环境(IDE)如:vs, vc++等,这些开发环境集成了编译,调试,使用起来很方便,而且图形化操作界面,简洁明了.但是在开 ...

  6. Wireshark win7 没有找到接口;找不到接口

    下载安装winpcap: https://www.winpcap.org/install/default.htm

  7. CentOS 7安装php

    我们已经在上一篇里安装上了nginx:现在我们想要php(也许还包括mysql). (CentOS 7里使用mariadb替代了mysql) # yum install mariadb-client ...

  8. 小程序https请求,http网站升到https

    最近开发小程序,因为以前只写过小程序的前端没注意接口,现在才发现原来所有的接口都必须使用https协议了,马上研究了一波,顺便也想给自己的博客升成https的. 申请免费证书 哈哈没办法就是喜欢免费的 ...

  9. 我是一个录像机(NVR)

    我是一个网络录像机,简称NVR.我的前辈是DVR,我们的区别很简单,DVR接的是模拟摄像机,我连接的是IP摄像机. 我的前辈DVR比我辛苦,因为模拟摄像机的模拟信号连过来之后,他要进行数字化.编码压缩 ...

  10. 解决Ubuntu中文显示为乱码

    1. 安装所需软件 sudo apt-get install zh-autoconvert sudo apt-get install zhcon 2. 配置系统 $ vi /var/lib/local ...