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

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

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

算法:来自leetcode caikehe

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

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

源码

  1. class Solution {
  2. public:
  3. vector<int> searchRange(vector<int>& nums, int target) {
  4. //binary find
  5. int index1 = binarySearch(nums, target);
  6. int index2 = binarySearch(nums, target+) - ;
  7. if(index1 < nums.size() && nums[index1] == target)
  8. return {index1, index2};
  9. return {-, -};
  10. }
  11. //二分变体
  12. int binarySearch(vector<int>& nums, int target)
  13. {
  14. int l = , r = nums.size()-;
  15. while(l <= r)
  16. {
  17. int mid = l + (r-l)/;
  18. if(nums[mid] < target)
  19. l = mid + ;
  20. else
  21. r = mid -;
  22. }
  23. return l;
  24. }
  25. };

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

  1. class Solution {
  2. public:
  3. vector<int> searchRange(vector<int>& nums, int target) {
  4. int len = nums.size();
  5. if(len <= )
  6. return {-, -};
  7. if(len == )
  8. if(nums[] == target)
  9. return {, };
  10. else
  11. return {-, -};
  12.  
  13. int l = , r = len-, mid;
  14. while(l <= r)
  15. {
  16. mid = l + (r-l)/;
  17. if(nums[mid] == target)
  18. break;
  19. else if(nums[mid] > target)
  20. r = mid - ;
  21. else
  22. l = mid + ;
  23. }
  24. if(nums[mid] != target)
  25. return {-, -};
  26.  
  27. l = mid, r = mid;
  28. while(l >= && nums[l] == nums[mid])
  29. l--;
  30. while(r < len && nums[r] == nums[mid])
  31. r++;
  32. return {l+, r-};
  33. }
  34. };

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. Linux输出信息并将信息记录到文件(tee命令)

    摘自:https://www.jb51.net/article/104846.htm 前言 最近工作中遇到一个需求,需要将程序的输出写到终端,同时写入文件,通过查找相关的资料,发现可以用 tee 命令 ...

  2. 关于Selenium remote模式分布式执行UI自动化测试必定面临的性能问题

    1.大部分自动化测试人员都是在本地执行UI自动化测试,也就是代码和浏览器必须在同一台机器上,这样的的缺陷很多,无法多任务并发执行UI自动化测试用例,效率极低 2.正是如此,Selenium 的remo ...

  3. html5调整音频音高

    var audioSrc = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/1715/the_xx_-_intro.mp3'; function buff ...

  4. 【Leetcode_easy】748. Shortest Completing Word

    problem 748. Shortest Completing Word 题意: solution1: class Solution { public: string shortestComplet ...

  5. 第十四章 SSL——《跟我学Shiro》

    目录贴:跟我学Shiro目录贴 对于SSL的支持,Shiro只是判断当前url是否需要SSL登录,如果需要自动重定向到https进行访问. 首先生成数字证书,生成证书到D:\localhost.key ...

  6. Vue + GraphQL初试

    基本用法 GraphQL概述 GraphQL基本语法特性 GraphQL类型系统 GraphQL类型系统内置基础类型 GraphQL类型系统内置修饰符 GraphQL工作原理 GraphQL执行过程 ...

  7. Opencv官方例程简介

    opencv sample文件夹例程 No1. adaptiveskindetector.cpp 利用HSV空间的色调信息的皮肤检测,背景不能有太多与肤色相似的颜色.效果不是特别好. No2. bag ...

  8. 通过js操作,将div设置为contenteditable的内容设为全选状态

    因为div设置为contenteitable可编辑的文本内容用 select()选择全部内容不生效,所以只能用下列方法: 先 creatTextRange或者 createRange <div ...

  9. oracle调用函数的方式

    --方法1.PLSQL代码块 SQL> set serveroutput onSQL> declare 2 v_sal emp_pl.sal%type; 3 begin 4 v_sal : ...

  10. Python习题006

    作业一:打印10*10  星星 ★☆ 要求一:普通打印★ l = 0 while l <10: h = 0 while h < 9: print("★", end=&q ...