34. Find First and Last Position of Element in Sorted Array + 二分
题意懒得抄了,大概是:在升序数组中给定整数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 + 二分的更多相关文章
- Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)
本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...
- 刷题34. Find First and Last Position of Element in Sorted Array
一.题目说明 题目是34. Find First and Last Position of Element in Sorted Array,查找一个给定值的起止位置,时间复杂度要求是Olog(n).题 ...
- [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 ...
- [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 ...
- (二分查找 拓展) 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 ...
- [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 ...
- 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 ...
- 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 ...
- 【LeetCode】34. Find First and Last Position of Element in Sorted Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...
随机推荐
- Linux输出信息并将信息记录到文件(tee命令)
摘自:https://www.jb51.net/article/104846.htm 前言 最近工作中遇到一个需求,需要将程序的输出写到终端,同时写入文件,通过查找相关的资料,发现可以用 tee 命令 ...
- 关于Selenium remote模式分布式执行UI自动化测试必定面临的性能问题
1.大部分自动化测试人员都是在本地执行UI自动化测试,也就是代码和浏览器必须在同一台机器上,这样的的缺陷很多,无法多任务并发执行UI自动化测试用例,效率极低 2.正是如此,Selenium 的remo ...
- html5调整音频音高
var audioSrc = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/1715/the_xx_-_intro.mp3'; function buff ...
- 【Leetcode_easy】748. Shortest Completing Word
problem 748. Shortest Completing Word 题意: solution1: class Solution { public: string shortestComplet ...
- 第十四章 SSL——《跟我学Shiro》
目录贴:跟我学Shiro目录贴 对于SSL的支持,Shiro只是判断当前url是否需要SSL登录,如果需要自动重定向到https进行访问. 首先生成数字证书,生成证书到D:\localhost.key ...
- Vue + GraphQL初试
基本用法 GraphQL概述 GraphQL基本语法特性 GraphQL类型系统 GraphQL类型系统内置基础类型 GraphQL类型系统内置修饰符 GraphQL工作原理 GraphQL执行过程 ...
- Opencv官方例程简介
opencv sample文件夹例程 No1. adaptiveskindetector.cpp 利用HSV空间的色调信息的皮肤检测,背景不能有太多与肤色相似的颜色.效果不是特别好. No2. bag ...
- 通过js操作,将div设置为contenteditable的内容设为全选状态
因为div设置为contenteitable可编辑的文本内容用 select()选择全部内容不生效,所以只能用下列方法: 先 creatTextRange或者 createRange <div ...
- oracle调用函数的方式
--方法1.PLSQL代码块 SQL> set serveroutput onSQL> declare 2 v_sal emp_pl.sal%type; 3 begin 4 v_sal : ...
- Python习题006
作业一:打印10*10 星星 ★☆ 要求一:普通打印★ l = 0 while l <10: h = 0 while h < 9: print("★", end=&q ...