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]
 
题目大意:
在给定的排序数组中寻找目标数出现的重复区间,如果没有这个数,则返回{-1,,1}
 
解法:
根据题目的时间复杂度采用二分查找进行搜索。
C++
class Solution {
public:
void searchRangeCore(vector<int>nums,int start,int end,int target,vector<int>&res){
if(end<start) return;
if(nums[start]>target||nums[end]<target) return;
int mid=(end-start)/2+start;
if(nums[mid]==target){
res[0]=(res[0]==-1?mid:min(res[0],mid));
res[1]=max(res[1],mid);
}
searchRangeCore(nums,start,mid-1,target,res);
searchRangeCore(nums,mid+1,end,target,res);
} vector<int> searchRange(vector<int>& nums, int target) {
vector<int>res={-1,-1};
searchRangeCore(nums,0,nums.size()-1,target,res); return res;
}
};

  

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. Oracle查看用户密码过期,修改永不过期

    01.查看当前open用户 select username,account_status,expiry_date,profile from dba_users; 02.查看目前的密码过期策略 sele ...

  2. 25-Python3 错误和异常

    25-Python3 错误和异常 ''' 语法错误 ''' # while True print('hello,runoob') ''' 异常 ''' ##ZeroDivisionError # pr ...

  3. python one

    哈哈,今天把它搞了 谁? Python啊! ..... *************************************** python:解释性语言,功能很强大,现在很有市场! & ...

  4. python我的tkinter学习,玩玩

    1.开始 #!/usr/bin/env python #coding:utf-8 import Tkinter ############################################ ...

  5. php 提取多维数组指定列

    前言:有时候在开发中会遇到这样的问题,我们需要把有规律的多维数组按照纵向(列)取出,有下面的方法可用: 我们将拿下面的数组来处理: 1 $arr = array( 2 '0' => array( ...

  6. HBase针对性问题汇总

    Q:    Hbase的rk设计,Hbase优化  a\rowkey:hbase三维存储中的关键(rowkey:行键 ,columnKey(family+quilaty):列键  ,timestamp ...

  7. js中var a=new Object()和var a={}有什么区别吗?

    应该是没有区别的,两者都是生成一个默认的Object对象.js和其它语言一样,一切对象的基类都是Object,所以,new Object()和简易的{}是同样的空对象,就是默认的对象.本来我以为{}应 ...

  8. spark 关联source

    IDEA就自动把jar包中的字节码反编译为Java源码,并且,我们可以直接下个断点调试程序,但是对于Scala,IDEA的反编译效果并不是很好,如下图所示: 2)提示“Source not found ...

  9. 17.Setters/getters

    知道类的成员变量何时因某种原因发生变化通常很有用.也可能需要以某种方式封装其访问. 为此,GDScript使用 setget 关键字提供了一个 setter/getter 语法.在变量定义后可直接使用 ...

  10. meta twitter 属性

    总结下国际范儿的meta标签 <meta name="A game made to inspire developers to use GSAP, ES6 and Flexbox&qu ...