思路:先二分查找到一个和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. 下载RPM常用网址

    网址一:http://www.rpm-find.net/linux/rpm2html/search.php   网址二:http://rpmfind.net/linux/rpm2html/search ...

  2. oracle的建表语句

    oracle数据库的建表语句,具体语法如下: CREATE TABLE tablename (column_name datatype [null,not null], column_name dat ...

  3. PLSQL Developer 12 注册码

    PLSQL Developer 12 注册码product code: 4vkjwhfeh3ufnqnmpr9brvcuyujrx3n3le serial Number:226959 password ...

  4. 获取Linux下的IP地址 java代码

    /** * 获取Linux下的IP地址 * * @return IP地址 * @throws SocketException */ public static String getLinuxLocal ...

  5. #leetcode刷题之路22-括号生成

    给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结果为:[ "((()))", "(()())&q ...

  6. vim8配置python3补全

    安装Python3 卸载编译安装的python3 rm -rf /usr/local/lib/python3.7/ rm -rf /usr/local/bin/2to3* rm -rf /usr/lo ...

  7. .net下redis和rabbitmq简单使用demo

    是参考 一下两篇博文整理了下. Redis:   https://www.cnblogs.com/5ishare/p/6492380.html RabbitMq:   https://www.cnbl ...

  8. laravel5.5源码阅读草稿——路由

    laravel 里的路由是由RouteServiceProvider提供的,其中的boot方法为启动项,调用了父类的boot方法. RouteServiceProvider中的boot方法设置了自己与 ...

  9. php向页面输出中文时出现乱码的解决方法

    今天,刚刚学习PHP发现用echo输出中文时,页面会出现乱码,然后查了一下资料说是浏览器编码格式有问题,要改成utf-8.但是每个人的浏览器编码可能会有所不同,所以找到了一个很好的解决方法, 就是在p ...

  10. JavaScript入门学习(2)--进度条

    <html> <style type="text/css"> #bar{width:0px; height:20px; background:#ee00ff ...