思路:先二分查找到一个和target相同的元素,然后再左边二分查找左边界,右边二分查找有边界。

class Solution {
public:
int begin = -, end = -;
int ends;
int lSearch(int left, int right, vector<int>& nums, int target)
{
if(left > right) return -;
int mid = (left + right) / ;
if(nums[mid] == target){
if(mid == || (mid > && nums[mid - ] < target)) return mid;
else return lSearch(left,mid - ,nums,target);
}else
return lSearch(mid + ,right,nums,target);
return -;
} int rSearch(int left, int right, vector<int>& nums, int target)
{
if(left > right) return -;
int mid = (left + right) / ;
if(nums[mid] == target){
if(mid == ends || (mid < ends && nums[mid + ] > target)) return mid;
else return rSearch(mid + ,right,nums,target);
}else
return rSearch(left ,mid - ,nums,target);
return -;
} int midSearch(int left, int right, vector<int>& nums, int target)
{
if(left > right) return -;
int mid = (left + right) / ;
if(nums[mid] == target){
return mid;
}
else if(nums[mid] < target) return midSearch(mid + ,right,nums,target);
else if(nums[mid] > target) return midSearch(left, mid - ,nums,target);
return -;
} vector<int> searchRange(vector<int>& nums, int target) {
ends = nums.size() - ;
int mid = midSearch(,ends,nums,target);
if(mid != -){
begin = lSearch(,mid,nums,target);
end = rSearch(mid,ends,nums,target);
}
vector<int> ans;
ans.push_back(begin);
ans.push_back(end);
return ans;
}
};

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. 刷题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

    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 在有序数组中查找元素的第一个和最后一个位置

    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 解题报告(Python & C++)

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

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

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

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

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

随机推荐

  1. Java解析Excel之应用Reflection等技术实现动态读取

    目录树 背景 技术选型 问题分析 技术要点及难点分析 源码分析 测试用例 背景 Tip:因为产品提的需求我都开发完了,进行了项目提测:前天老大走过来说:你用spring-boot开发一个解析Excel ...

  2. ES module 实现方式

    随着js社区不断发展,js功能更加强大,细数js的几种 module 方式. 整理了七种模块化方式 1.作为新手,练习小的demo,比较喜欢的方式.不适合大的项目. <!--html--> ...

  3. let与var的区别,为什么什么要用let?

    1.var是全局声明,let是块级作用的,只适用于当前代码块 var a = 1: if(true){ let a; a=22: console.log(a);'//22 } if(){}内就是let ...

  4. NOIP2018 Day2毒瘤题目

    Day2毒瘤题目 T1:travel 拿到题目,一看,图论,完了...... 仔细看了看题目,诶这个不是dfs序么?当场敲出dfs.跑样例一,过了,结果一跑样例二,当场废掉.(样例二有环,会跑不出正解 ...

  5. mysql截取字段并插入到新的字段中

    例如:在产品表product表中字段content值为["10"],然后在产品表中新建一个字段product_id,提出字段content的值10,如何实现呢? 解: update ...

  6. tomcat 远程debug配置,教你远程调试代码,解决线上故障

      IDEA远程DEBUG Tomcat很简单,配置如下: 1.修改tomcat服务器配置 打开tomcat/bin/catalina.sh 在空白处添加如下参数 CATALINA_OPTS=&quo ...

  7. Vue基础学习(纯属个人学习的笔记,慢慢新增)

       1.在html文件中,声明了template对象,那么在 data对象中的v-html和v-text的绑定数据是不起作用的 2.v-的几个常用绑定 v-html和v-text:引用的conten ...

  8. linux-2.6.22.6内核启动分析之Makefile文件

    学习目标 分析Makefile文件,了解内核中的哪些文件被编译,如何被编译,连接时顺序如何确定! Linux内核源码中包含很多的Makefile文件,这些Makefile文件又包含其它的一些文件,比如 ...

  9. celery知多少

    Celery 1.什么是Celery Celery是一个简单.灵活且可靠的,处理大量消息的分布式系统 专注于实时处理的异步任务队列 同时也支持任务调度 Celery架构 Celery的架构由三部分组成 ...

  10. Python3 透明网桥算法

    import time #定义网桥1 b1 = {} port_list1 = [1, 2] #主机列表 L1 = ['a','b','c'] L2 = ['d','e'] L = [L1,L2] d ...