题意懒得抄了,大概是:在升序数组中给定整数target,找到第一个和最后一个target的索引,找到返回{index1, index2},否则返回{-1, -1};

时间复杂度要求:O(logn)

分析:要求对数时间,又是查找,我们不难想到二分查找。但是有一点,怎么查到第一个和最后一个呢?这困扰了我。

算法:来自leetcode caikehe

1. 使用二分变体,查找target作为index1,再找target+1, 作为index2;

2. 对index做判断,如果它未越界且它的对应值等于target则返回{index1, index2-1};否则,返回{-1, -1};

源码

class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
//binary find
int index1 = binarySearch(nums, target);
int index2 = binarySearch(nums, target+) - ;
if(index1 < nums.size() && nums[index1] == target)
return {index1, index2};
return {-, -};
}
//二分变体
int binarySearch(vector<int>& nums, int target)
{
int l = , r = nums.size()-;
while(l <= r)
{
int mid = l + (r-l)/;
if(nums[mid] < target)
l = mid + ;
else
r = mid -;
}
return l;
}
};

一个较为易于理解的算法(时间复杂度可能略高于两次二分)

 class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
int len = nums.size();
if(len <= )
return {-, -};
if(len == )
if(nums[] == target)
return {, };
else
return {-, -}; int l = , r = len-, mid;
while(l <= r)
{
mid = l + (r-l)/;
if(nums[mid] == target)
break;
else if(nums[mid] > target)
r = mid - ;
else
l = mid + ;
}
if(nums[mid] != target)
return {-, -}; l = mid, r = mid;
while(l >= && nums[l] == nums[mid])
l--;
while(r < len && nums[r] == nums[mid])
r++;
return {l+, r-};
}
};

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_Easy tag: Binary Search

    Description Given a sorted array of n integers, find the starting and ending position of a given tar ...

  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 && lintcode 61. Search for a Range

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  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

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  8. 34. Find First and Last Position of Element in Sorted Array (JAVA)

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

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

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

随机推荐

  1. 阶段5 3.微服务项目【学成在线】_day09 课程预览 Eureka Feign_11-课程详情页面静态化-课程信息模板设计

    测试模板 页面的模板已经做好了 直接那来用 测试模板,把这个模板copy到这个测试freemarker的resources目录 远程调用 获取数据 返回模板的文件名 tomcat不支持ssr.ngin ...

  2. 算法习题---4-1象棋(UVa1589)

    一:题目 在黑方只有一个“将”的情况下,红方只有(车.马.炮)(可以多个).帅的情况下,判断黑方是否被将死 (一)题目详解 其中棋盘按照坐标方式表示,左上角为(,),列数最大9,行数最大10 G 表示 ...

  3. 【419】C语言语句

    判断语句 C 语言提供了以下类型的判断语句.点击链接查看每个语句的细节. 语句 描述 if 语句 一个 if 语句 由一个布尔表达式后跟一个或多个语句组成. if...else 语句 一个 if 语句 ...

  4. 三个线程abc顺序执行

    1.使用synchronized悲观锁(秋招阿里的一个笔试题,应该写的比较复杂,然后就没有然后了o(╥﹏╥)o) public class ThreadThreadp { private int fl ...

  5. C#将HTML代码转换为图片

    前端通过富文本控件接收到了一段html代码,后端想通过图片的形式展示到另外的地方,这种情况怎么处理呢.直接上代码: using System; using System.Collections.Gen ...

  6. Apache配置参数的优化

    查看apache开启那些模块: apachectl -t -D DUMP_MODULES 1)KeepAlive On/Off KeepAlive指的是保持连接活跃,换一句话说,如果将KeepAliv ...

  7. ELK之在CentOS7.5上使用rpm包安装配置ELK7版本

    一,安装环境查看 二,软件版本选用 jdk 1.8.0_171 elasticsearch 7.1.1 kibana 7.1.1 logstash 7.1.1 三,安装配置 1,安装JDK 过程不详述 ...

  8. iOS-CGAffineTransform相关函数

    CGAffineTransform相关函数 CGAffineTransformMakeTranslation(width, 0.0);是改变位置的,CGAffineTransformRotate(tr ...

  9. 【Kail 学习笔记】自用KAIL更新源

    打开 /etc/apt/source.list 添加-保存即可 deb http://mirrors.ustc.edu.cn/kali kali-rolling main non-free contr ...

  10. 帝国cms 批量替换语句

    UPDATE www_92game_net_cnys_ecms_caipu SET titlepic=REPLACE(titlepic,'http://file.92game.net', '') ; ...