思路:先二分查找到一个和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. 转发: Angular装饰器

    Angular中的装饰器是一个函数,它将元数据添加到类.类成员(属性.方法)和函数参数. 用法:要想应用装饰器,把它放在被装饰对象的上面或左边. Angular使用自己的一套装饰器来实现应用程序各部件 ...

  2. react 配置开发环境

    一:先自行下载安装node和npm 二:cnpm install create-react-app -g 三:create-react-app my-project 四:cd my-project  ...

  3. vue 复习(2)v-bind的应用 v-bind:classv-binf:style

    dasdclass与style绑定v-bind 1. 绑定HTML Class 对象语法 有些时候我们想动态的切换class的类名.在原生的js或jq中我们就要通过事件来动态的改变class类名,但在 ...

  4. #leetcode刷题之路47-全排列 II

    给定一个可包含重复数字的序列,返回所有不重复的全排列.示例:输入: [1,1,2]输出:[ [1,1,2], [1,2,1], [2,1,1]] 之前的https://www.cnblogs.com/ ...

  5. code#5 P4 逻辑树

    逻辑树   时间限制: 3.0 秒 空间限制: 512 MB 相关文件: 题目目录 题目描述 有一棵树,叫逻辑树. 这个树有根,有 2N−1 个节点,其中 N 个叶子,每个非叶节点恰好有两个孩子. 每 ...

  6. 使用babel

    1).Babel支持NPM包形式的安装,打开命令行窗口,切换到项目根目录,命令如下 npm install babel-cli 2).安装成功后,在package.json文件里添加如下代码 &quo ...

  7. Python学习 :多线程 --- 锁

    多线程 什么是锁? - 锁通常被用来实现对共享资源的同步访问. - 为每一个共享资源创建一个Lock对象,当你需要访问该资源时,调用acquire方法来获取锁对象(如果其它线程已经获得了该锁,则当前线 ...

  8. Go语言中结构体的使用-第2部分OOP

    1 概述 结构体的基本语法请参见:Go语言中结构体的使用-第1部分结构体.结构体除了是一个复合数据之外,还用来做面向对象编程.Go 语言使用结构体和结构体成员来描述真实世界的实体和实体对应的各种属性. ...

  9. 快排(python)

    用python写了个快排,第一次发现python居然可以这么简洁. def quicksort(arr): if len(arr) <= 1: return arr pivot = arr[le ...

  10. ubuntu下python在pycharm环境下安装setuptools和pip,和distutils.core

    python安装好后,我们用pycharm安装所需的第三方模块时,出现“Python packaging tools not found. install packaging tools”点击安装输完 ...