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 ...
随机推荐
- 03--STL算法(常用算法)
一:常用的查找算法 (一)adjacent_find():邻接查找 在iterator对标识元素范围内,查找一对相邻重复元素,找到则返回指向这对元素的第一个元素的迭代器.否则返回past-the-en ...
- java获取两个日期之间的所有日期
java获取两个日期之间的所有日期 解决方法: 1.核心方法 private List<String> getBetweenDates(String start, String end ...
- LeetCode_172. Factorial Trailing Zeroes
172. Factorial Trailing Zeroes Easy Given an integer n, return the number of trailing zeroes in n!. ...
- Tips for vcpkg
概述 vcpkg是微软开发的在Windows, Linux和MacOS平台管理C/C++库的开源工具. 快速开始 要求 使用vcpkg需满足如下条件: Windows 10, 8.1, 7, Linu ...
- PHP重建数组的索引
sort() array_merge()跟一个空数组合并都可以重建索引数组的键(key)
- win7下安装IIS7
在Windows 7下如何安装IIS7,以及IIS7在安装过程中的一些需要注意的设置,以及在IIS7下配置ASP的正确方法. 在Windows 7下面IIS7的安装方法: 一.进入Windows 7的 ...
- 【Leetcode_easy】784. Letter Case Permutation
problem 784. Letter Case Permutation 参考 1. Leetcode_easy_784. Letter Case Permutation; 2. Grandyang; ...
- iOS-类似微信摇一摇
首先,一直以为摇一摇的功能实现好高大上,结果百度了.我自己也模仿写了一个demo.主要代码如下: 新建一个项目,名字为AnimationShake. 主要代码: - (void)motionBegan ...
- django:bootstrap table加载django返回的数据
bootstrap table加载表格数据有两类方式: 一种通过data属性的方式配置,一种是javascipt方式配置 这里看js配置方式: 1.当数据源为.json文件时 url参数写上json文 ...
- Javascript的原型链与继承
目录 1. ES5最经典的寄生组合式继承图 2. ES5和ES6的继承 Javascript语言的继承机制,它没有"子类"和"父类"的概念,也没有"类 ...