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 ...
随机推荐
- 相机标定与矫正opencv+MATLAB
博客转载自:http://blog.csdn.net/Loser__Wang/article/details/51811347 本文目的在于记录如何使用MATLAB做摄像机标定,并通过opencv进行 ...
- LeetCode 刷题笔记 155. 最小栈(Min Stack)
tag: 栈(stack) 题目描述 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素 ...
- 【坚持】Selenium+Python学习之从读懂代码开始 DAY4
2018/05/21 [生成器详解:廖雪峰的官方网站](https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d ...
- 搭建 Digital Ocean 服务器
作为程序员,搭建属于自己的服务器可以部署自己的网站,可以配置代理***,了解国际前沿科技:一些高性能的服务器还可以完成一些云计算.深度学习模型的训练任务. DigitalOcean 服务启动 注册 D ...
- 半年收入超2亿RMB 独立游戏开发者的艰苦创业路
一款叫做<监狱建筑师>的模拟经营游戏,目前在Steam平台获得了3000万美元(近2亿元)以上的收入.这款游戏由英国独立工作室Introversion Software发布,而团队最困难的 ...
- [linux] 利用PROMPT_COMMAND实现命令审计
网上查了实现命令审计大概有以下几种: 查不到了,改天再补充 以下环境基于CentOS 6 1.修改history时间格式 echo 'HISTTIMEFORMAT="%F %T " ...
- 模块-Memcached、Redis
目录 Mecache 安装 使用 Redis 安装 Python操作Redis 操作模式 连接池 操作 String Hash List Set sort set 其他常用操作 管道 发布订阅 sen ...
- C++ 函数 内联函数
内联函数的功能和预处理宏的功能相似,在介绍内联函数之前,先介绍一下预处理宏.宏是简单字符替换,最常见的用法:定义了一个代表某个值的全局符号.定义可调用带参数的宏.作为一种约定,习惯上总是用大写字母来定 ...
- ffmpeg——压缩mav格式音频
今天偶然帮朋友压缩一个mav格式的音频.开始用压缩码率的方式,mav格式的音频体积一点都没变,查资料需要压缩音频文件的采样率和声道才能压缩mav格式的音频. 压缩要求是:将一个mav格式的音频文件,由 ...
- oracle将多个结果集用逗号拼接成字符串
有两个函数wmsys.wm_concat和listagg 1,SELECT wmsys.wm_concat(CATALOG_NAME) FROM "DATASHARE"." ...