题意懒得抄了,大概是:在升序数组中给定整数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. ubuntu18.04 install rar

    sudo apt-get update #如果好久没有更新资源建议update一次 sudo apt-get install rar #安装rar sudo apt-get install unrar ...

  2. 采购信息记录批导BAPI

    转自:https://www.cnblogs.com/freeandeasy/p/11810272.html作者的话:   可以批导创建及修改信息记录的主数据.而且可以对条件中的时间段及其数量等级中的 ...

  3. c#子线程执行完怎么通知主线程(转)

    定义一个委托实现回调函数 public delegate void CallBackDelegate(string message); 程序开始的时候 //把回调的方法给委托变量 CallBackDe ...

  4. ElasticSearch——Curator索引管理

    简介 curator 是一个官方的,可以管理elasticsearch索引的工具,可以实现创建,删除,段合并等等操作.详见官方文档 功能 curator允许对索引和快照执行许多不同的操作,包括: 从别 ...

  5. Win10使用Tex Live和VS Code和Latex Workshop插件编写Latex文档(未完成版本)

    首先取Tex Live官网下载安装包:https://www.tug.org/texlive/acquire-netinstall.html 我下载的是 http://mirror.ctan.org/ ...

  6. iOS-浅谈iOS中三种生成随机数方法

    ios 有如下三种随机数方法:

  7. Docker 容器的资源限制 cgroup(九)

    目录 一.cgroup简介 二.CPU资源配额控制 1.CPU份额控制 2.CPU周期控制 3.CPU core控制 4.CPU配额控制参数的混合使用 二.对内存的限额 三.对 Block IO 的限 ...

  8. 如何理解AWS 网络,如何创建一个多层安全网络架构

    目录 一.要求 网络架构图 网络各组件关系 二.操作步骤 2.1.网络设置 2.2.安全设置 2.3.创建实例 三.费用 3.1.NAT 网关费用 一.要求 创建一个三层网络架构,服务器只能通过跳板机 ...

  9. Spring 本地事务

    主要原理: 事务的顶层接口: PlatformTransactionManager提供了3个方法,在申明式事务中这3个方法都是通过aop 调用的: 1.getTransaction: 进入相关需要被拦 ...

  10. ipad 没有数据线如何上传文件到局域网windows PC 的解决方案

    是的,ios 的封闭性,真麻烦,不想用数据线,还不想用iTunes ,那你找对了. 方案一: (好像只能上传文件,不能下载,能在线查看媒体.) 我的想法是在Windows建立一个http file s ...