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 integers nums sorted in ascending order, find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1].
Example 1:
Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]
Example 2:
Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1,-1]
解法
二分法查找
class Solution
{
public:
vector<int> searchRange(vector<int>& nums, int target)
{
vector<int> target_pos(2, -1);
int min = 0;
int max = nums.size() - 1;
int mid = 0;
int t = 0;
while(min <= max)
{
mid = (min + max) / 2;
t = nums[mid];
if (t == target)
{
target_pos.clear();
int pre = mid - 1;
bool have = false;
while(pre >= min && nums[pre] == target)
{
have = true;
--pre;
}
if (have) target_pos.push_back(++pre);
else target_pos.push_back(mid);
have = false;
pre = mid + 1;
while (pre <= max && nums[pre] == target)
{
have = true;
++pre;
}
if (have) target_pos.push_back(--pre);
else target_pos.push_back(mid);
break;
}
else if (t > target)
{
max = mid - 1;
}
else
{
min = mid + 1;
}
}
return target_pos;
}
};
时间复杂度: O(logn).
空间复杂度: O(1).
leetcode-algorithms-34 Find First and Last Position of Element in Sorted Array的更多相关文章
- 【LeetCode】34. Find First and Last Position of Element in Sorted Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...
- Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)
本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...
- 乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array
乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array 一.前言 这次我们还是要改造二分搜索,但是想法却 ...
- 刷题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 在有序数组中查找元素的第一个和最后一个位置
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_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 && 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 ...
- leetcode个人题解——#34 Find First and Last Position of Element in Sorted Array
思路:先二分查找到一个和target相同的元素,然后再左边二分查找左边界,右边二分查找有边界. class Solution { public: , end = -; int ends; int lS ...
随机推荐
- 谷歌大规模机器学习:模型训练、特征工程和算法选择 (32PPT下载)
本文转自:http://mp.weixin.qq.com/s/Xe3g2OSkE3BpIC2wdt5J-A 谷歌大规模机器学习:模型训练.特征工程和算法选择 (32PPT下载) 2017-01-26 ...
- 常用linux,DOS命令——持续更新
cd 文件夹名 进入某个文件夹 cd ../ 退出该级目录进入上一级 cd ../../ 退出该级目录进入上上级 cd ../../demo 退出该级目录进入上上级的目录 d: 回车 进入d盘 ls ...
- 日系插画学习笔记(一):SAI软件基础
检测驱动是否安装正确:1.画笔没有压感,线条没有粗细变化2.画笔线条有锯齿 一.文件:新建文件:预设尺寸:一般选择A3(8k),A4(16k),A5(32k)作业要求:A4A5 - 300dpi,像素 ...
- algorithm.sty not found error in LaTeX 解决方法
参考: algorithm.sty not found error in LaTeX algorithm.sty not found error in LaTeX 解决方法 错误日志: LaTeX E ...
- 聚类算法——KMEANS算法
聚类概念 无监督问题:我们手里没有标签 聚类:相似的东西分到一组 难点:如何评估,如何调参 基本概念 要得到簇的个数,需要指定K值 质心:均值,即向量各维取平均即可 距离的度量:常用欧几里得距离和余弦 ...
- 1月4日笔记 vi编辑器
Penn vi编辑器,全称是visual interface,可以执行输出.删除.查找.替换等众多的文本操作. vi并不是一个排版程序,不可以对字体.格式.段落等其他的属性进行编排. vi是全 ...
- 关于在mac安装安卓的模拟器的一些些那点事情~~~
ook~~自己捣鼓了三天终于安装成功了~妹的~踩了太多的坑~整个人就不好了~ 为了节省大家的时间~所以今天我就将我安装的过程整体思路教给大家!有了思路安装起来就很好了!!!但是 注意的是,也许你会出现 ...
- pom中配置的仓库无效的问题
今天在用spring cloud的时候发现,配置的pom仓库一直无效(官网要求2.0版本直接从指定仓库里下).于是上网搜索,发现(http://18810098265.iteye.com/blog/2 ...
- [原][unreal][UE][spark]分析unreal engine 虚幻引擎的粒子编辑器:Cascade
参考:https://www.raywenderlich.com/270-unreal-engine-4-particle-systems-tutorial (使用了一个飞机射击游戏的粒子来展示,全英 ...
- 使用Hexo搭建一个简单的博客(二)
昨天想着用Hexo和github搭一个自己简单的博客,记录一下自己踩过的坑,具体的流程就不重复了,主要参考了一下几篇文章 GitHub+Hexo 搭建个人网站详细教程 使用Hexo+Github一步步 ...