题意懒得抄了,大概是:在升序数组中给定整数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. Qt编写自定义控件53-自定义宽高下拉框

    一.前言 默认的qcombobox控件,如果元素item中的内容过长超过控件本身的宽度的话,会自动切掉变成省略号显示,有些应用场景不希望是省略号显示,希望有多长就显示多长,还有一种应用场景是需要设置下 ...

  2. linux常用的命令和工具

    screen     管理会话工具 与之相似的工具还有tmux # screen // 进入一个回话 .还可以给会话取名 screen -S modify_screen #vim screen.txt ...

  3. jsPlumb 基本概念

    jsPlumb 基本概念 一.默认属性 Anchor:锚点(连接点位置),可以设置在任何没有锚点的目标上(endPoint) Anchors:设置在connect的源和目标点的连接点位置,默认是 Bo ...

  4. socket 异步接收连接和接收数据

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. 【c# 学习笔记】构造函数

    构造函数 主要用于创建类的实例对象.当调用构造函数创建一个对象时,构造函数会为对象分配内存空间,并初始化类的成员.构造函数分为实例构造函数和静态构造函数两种. 1.实例构造函数 实例构造函数用于创建和 ...

  6. .rpt 文件怎麽打開?Crystal Reports 打開.rpt失敗 ?

    .rpt   是SAP Crystal Reports 製作出來的檔案的後綴名, 可以通過 SAP Crystal Reports Viewer 打開,也可以直接用記事本(notepad / Note ...

  7. VS2010开发.cpp与.c的注意事项

    VS2010开发.cpp与.c的注意事项 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 强大的VS2010,正是由于vs2010的完全封装,让现在的wind ...

  8. MySQL初始化脚本mysql_install_db使用简介及选项参数

    mysql_install_db是一个默认放在.../mysql/scripts的一个初始化脚本. 该脚本可以在任何装有perl的操作系统上被使用,在5.6.8之前的版本,该脚本是一个shell脚本, ...

  9. 如何使用js实现轮播图

    <!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" ...

  10. 关于AES加密,以及各种分组加密

    http://blog.csdn.net/searchsun/article/details/2516191