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 ...
随机推荐
- OpenGL学习笔记(3) 纹理
关于纹理 一般游戏里的物体不一定都是纯色的物体,物体上面会有一些图片贴在上面,比如墙壁,箱子,地板,可以看到砖头.木板和大理石组成的图片,要把图片贴到计算机里的几何图形的话,就要把图片的颜色采样贴到几 ...
- 有关JOIN ON的心得体会
在第一家公司工作大概有一年之后,我的上司开始让我负责一个项目了. 说起这个项目,其实就是类似一个报表系统的抽数据的活.我的主要工作就是将我们公司产生的数据进行抽取清理,然后生成一些带有分析性质的数据. ...
- 高可用OpenStack(Queen版)集群-1. 集群环境
参考文档: Install-guide:https://docs.openstack.org/install-guide/ OpenStack High Availability Guide:http ...
- 51nod-1298 圆与三角形(计算几何超详解)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1298 给出圆的圆心和半径,以及三角形的三个顶点,问圆同三角形是 ...
- 初学node.js-nodejs安装运行(1)
1.Node.js中文官网http://nodejs.cn/download/下载node.js 学习node.js需要有javascript基础,没有基础的可以在http://www.w3schoo ...
- jumpserver安装与部署
1.简介 Jumpserver 是一款由Python编写开源的跳板机(堡垒机)系统,实现了跳板机应有的功能.基于ssh协议来管理,客户端无需安装agent.特点: 完全开源,GPL授权 Pyth ...
- exit命令详解
基础命令学习目录首页 原文链接:https://www.cnblogs.com/itcomputer/p/4157859.html 用途说明 exit命令用于退出当前shell,在shell脚本中可以 ...
- Python交互数据库(Mysql | Mongodb | Redis)
数据库 Mysql Mysql MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,后来被Sun公司收购,Sun公司后来又被Oracle公司收购,目前属于Oracle旗下产品 MyS ...
- Dao DaoImp
DAO层:DAO层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DAO的接口,然后在Spring的配置文件中定义此接口的实现类,然后就可在模块中调用此接口 ...
- 学习Mybatis的两个必须的jar包分享
百度云盘:http://pan.baidu.com/s/1nuNxRcd 提取码:t765(好像不需要提取码,不太会用云盘...) 自己学习mybatis的时候去找这两个jar包也是不容易,特别分享一 ...