Find First and Last Position of Element in Sorted Array - LeetCode
题目链接
Find First and Last Position of Element in Sorted Array - LeetCode
注意点
nums可能为空- 时间复杂度为O(logn)
解法
解法一:最普通的二分搜索,先找到一个target,然后向两边拓展。
class Solution {
public:
int binarySearch(vector<int>& nums, int target)
{
int left = 0,right = nums.size()-1;
while(left <= right)
{
int mid = left + (right-left)/2;
if(nums[mid] == target) return mid;
if(nums[mid] < target) left = mid+1;
else right = mid-1;
}
return -1;
}
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> ret;
int index = binarySearch(nums,target);
int left = index,right = index;
while(left > 0 && nums[left-1] == nums[index]) --left;
while(right < nums.size()-1 && nums[right+1] == nums[index]) ++right;
ret.push_back(left);
ret.push_back(right);
return ret;
}
};

解法二:解法一在最坏情况下时间复杂度是O(n),比如整个nums都是target。因此我们用两次二分搜索,第一次找到第一个等于target的数字,第二次找到最后一个等于target的数字。时间复杂度O(logn)
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> ret;
int left = 0,right = nums.size()-1;
while(left <= right)
{
int mid = left+(right-left)/2;
if(nums[mid] >= target) right = mid-1;
else left = mid+1;
}
if(left >= 0 && left < nums.size() && nums[left] == target) ret.push_back(left);
else return {-1,-1};
left = 0;right = nums.size()-1;
while(left <= right)
{
int mid = left+(right-left)/2;
if(nums[mid] <= target) left = mid+1;
else right = mid-1;
}
if(right >= 0 && right < nums.size() && nums[right] == target) ret.push_back(right);
return ret;
}
};

小结
- 二分搜索变种题
Find First and Last Position of Element in Sorted Array - LeetCode的更多相关文章
- Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)
本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...
- leetcode-algorithms-34 Find First and Last Position of Element in Sorted Array
leetcode-algorithms-34 Find First and Last Position of Element in Sorted Array Given an array of int ...
- 乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array
乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array 一.前言 这次我们还是要改造二分搜索,但是想法却 ...
- [LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)
原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an ...
- 刷题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: Find First and Last Position of Element in Sorted Array
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- [Swift]LeetCode34. 在排序数组中查找元素的第一个和最后一个位置 | 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 ...
随机推荐
- 使用Xshell远程访问tensorboard
在使用tensorflow时,由于本地资源的限制,一般在远程服务器上训练模型,而服务器没有图形界面,那么在训练过程中如何实时地访问tensorboard可视化数据呢? 如果服务器和本地电脑连接在同一个 ...
- jmeter控制器(二)
循环控制器: 顾名思义就是做循环控制的,与线程组的循环一样的,不过这里的循环控制器是用在一个单独的模块的,而在线程组里面的循环是作用于全局的.循环控制器里面设置的循环次数是局部有效,只控制自己范围内的 ...
- AssertionError
(1)p1 = multiprocessing.Process(test1)p2 = multiprocessing.Process(target=test2) 错误: p1缺少target,应为(t ...
- 07-matplotlib-箱线图
import numpy as np import matplotlib.pyplot as plt ''' 箱形图(Box-plot)又称为盒须图,盒式图,或 箱线图: 是一种用在显示一组数据分散情 ...
- Centos7.2构建Python3.6开发环境
1.安装python3.6 1.这里使用一台全新的腾讯云主机,首先获取linux系统版本信息. [root@VM_46_121_centos ~]# cat /etc/redhat-release C ...
- python操作hive并且获取查询结果scheam
执行hive -e 命令并且获取对应的select查询出来的值及其对应的scheam字段 需要在执行语句中前部添加 set hive.cli.print.header=true; 这个设置,如下语句: ...
- 将本地开发完的SDK代码上传到SVN上面:an error occurred while contacting the repository The server may be unreachable or the URL may be incorrect
将本地开发完的SDK代码上传到SVN上面:an error occurred while contacting the repository The server may be unreachabl ...
- BugPhobia开发篇章:Scurm Meeting-更新至0x03
0x01 :目录与摘要 If you weeped for the missing sunset, you would miss all the shining stars 索引 提纲 整理与更新记录 ...
- 2-Tenth Scrum Meeting20151210
任务分配 闫昊: 今日完成:请假.(编译) 明日任务:参加会议讨论,安排任务分工. 唐彬: 今日完成:请假.(编译) 明日任务:参加会议讨论,安排任务分工. 史烨轩: 今日完成:请假.(编译) 明日任 ...
- Linux 基础入门第一次实验笔记
第一节.实验介绍 本节主要介绍 Linux 的历史,Linux 与 Windows 的区别等入门知识.如果你已经有过充分的了解,可以跳过本节,直接进入下一个实验. 一.Linux 为何物 Linux ...