leetcode - 34. Search for a Range - Medium

descrition

Given an array of integers 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].

For example,

Given [5, 7, 7, 8, 8, 10] and target value 8,

return [3, 4].

解析

对于有序数组的查找问题,基本上都可以使用折半查找的思路。再者题目的要求复杂度是 O(log n),因此我们需要两次折半查找。如果我们只用一次折半查找,找到数组 target 出现的任意一个位置,然后线性遍历找到最左边和最右,需要的复杂度是 O(n)。

具体实现代码如下。

code


#include <iostream>
#include <vector>
#include <algorithm> using namespace std; class Solution{
public:
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> ans(2, -1);
int ileft = binarySearchLeftMost(nums, target);
if(ileft < 0)
return ans; int iright = binarySearchRightMost(nums, target); ans[0] = ileft;
ans[1] = iright; return ans;
} int binarySearchLeftMost(vector<int>& nums, int target){
int ans = -1; // save the left most index in nums which equal to target
int ileft = 0, iright = nums.size() - 1; while(ileft <= iright){
int imid = ileft + (iright - ileft) / 2;
if(nums[imid] == target){
ans = imid;
iright = imid - 1;
}else if (nums[imid] < target){
ileft = imid + 1;
}else{
// nums[imid] > target
iright = imid - 1;
}
} return ans;
} int binarySearchRightMost(vector<int>& nums, int target){
int ans = -1;
int ileft = 0, iright = nums.size() - 1; while(ileft <= iright){
int imid = ileft + (iright - ileft) / 2;
if(nums[imid] == target){
ans = imid;
ileft = imid + 1;
}else if (nums[imid] < target){
ileft = imid + 1;
}else {
// nums[imid] > target
iright = imid - 1;
}
} return ans;
} }; int main()
{
return 0;
}

[array] leetcode - 34. Search for a Range - Medium的更多相关文章

  1. [LeetCode] 034. Search for a Range (Medium) (C++/Java)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...

  2. [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 ...

  3. leetCode 34.Search for a Range (搜索范围) 解题思路和方法

    Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...

  4. leetcode 34 Search for a Range(二分法)

    Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...

  5. LeetCode 34. Search for a Range (找到一个范围)

    Given an array of integers sorted in ascending order, find the starting and ending position of a giv ...

  6. leetcode@ [34] Search for a Range (STL Binary Search)

    https://leetcode.com/problems/search-for-a-range/ Given a sorted array of integers, find the startin ...

  7. [leetcode 34] search for a range

    1 题目: Given a sorted array of integers, find the starting and ending position of a given target valu ...

  8. Java [leetcode 34]Search for a Range

    题目描述: Given a sorted array of integers, find the starting and ending position of a given target valu ...

  9. LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)

    题目链接: https://leetcode.com/problems/search-for-a-range/?tab=Description   Problem: 在已知递减排序的数组中,查找到给定 ...

随机推荐

  1. Java实现Html转PDF的方法

    写在前面 以下路径问题根据项目结构自己修改,以下是我使用spring boot打成jar包的写法. 一.需求背景 在前端编辑器中输入任意的文本,包括css样式变化,保存为html文本.通过Java后台 ...

  2. VMware虚拟机安装教程

    在实际的开发过程中,使用到VMware的时候是很多的.当你的电脑安装的是windows系统而想使用linux系统时,为了避免对本机的系统进行操作,那么安装虚拟机就是一项不错的选择. 在写这篇博文时,刚 ...

  3. ShoneSharp语言(S#)的设计和使用介绍—数值Double

    ShoneSharp语言(S#)的设计和使用介绍 系列(5)- 数值Double 作者:Shone 声明:原创文章欢迎转载,但请注明出处,https://www.cnblogs.com/ShoneSh ...

  4. # WPF动画速率效果

    在WPF中使用动画的情况非常多,而要让动画变得生动往往要使用一些变速动画,WPF也内置了很方便的缓动函数来实现这一功能. 除此之外,WPF还有关键帧动画,利用关键帧动画能够很好的控制动画的细节,与美工 ...

  5. Error parsing column 8 (IsRecommended=0 - SByte) Dapper查询mysql数据库可空的tinyint(1)一个错误

    出错条件: 1.实体属性为bool?类型 2.对应字段为可空的tinyint(1)类型 3.该字段查询结果内即含有null,又含有正常值 google答案,两种建议: 1.修改sql语句,直接cast ...

  6. 迭代子模式(Iterator)

    迭代子模式(Iterator) 顾名思义,迭代器模式就是顺序访问聚集中的对象,一般来说,集合中非常常见,如果对集合类比较熟悉的话,理解本模式会十分轻松.这句话包含两层意思:一是需要遍历的对象,即聚集对 ...

  7. JavaScript内置的预定义函数

    javascript引擎中有一组可供随时调用的内建函数.这些内建函数包括 parseInt()  将收到的任何输入值转换成整数类型输出,如果转换失败,返回NaN parseFloat() 功能基本与p ...

  8. PAT 1008. Elevator (20)

    1008. Elevator (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The highest ...

  9. 数据库服务器---Qps

    QPS(Query Per Second)意思为"每秒查询率",是一台服务器每秒能够相应的查询次数,是对一个特定的查询服务器在规定时间内所处理流量多少的衡量标准.同时也代表一种计算 ...

  10. gis电子地图开发公司面临的挑战和机遇

    从上个世纪90年代开始电子地图应用就已经收到人们的关注,但是由于时代的局限性和市场经济发展的不成熟.地理信息系统系统的应用并没有得到很好的利用.只有少数的国家机构和军事系统才能够使用这些应用.随着技术 ...