[array] leetcode - 34. Search for a Range - Medium
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的更多相关文章
- [LeetCode] 034. Search for a Range (Medium) (C++/Java)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
- [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 ...
- 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 ...
- 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 ...
- LeetCode 34. Search for a Range (找到一个范围)
Given an array of integers sorted in ascending order, find the starting and ending position of a giv ...
- 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 ...
- [leetcode 34] search for a range
1 题目: Given a sorted array of integers, find the starting and ending position of a given target valu ...
- Java [leetcode 34]Search for a Range
题目描述: Given a sorted array of integers, find the starting and ending position of a given target valu ...
- LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)
题目链接: https://leetcode.com/problems/search-for-a-range/?tab=Description Problem: 在已知递减排序的数组中,查找到给定 ...
随机推荐
- jq获取浏览器的高度
// console.log("2-"+$(window).height()); //浏览器当前窗口可视区域高度 // console.log("3-"+$(d ...
- Python 爬虫练习(二)爬取补天公益SRC厂商域名URL (2017年11月22日)
介绍下: 补天是国内知名的漏洞响应平台,旨在企业和白帽子共赢. 白帽子在这里提交厂商漏洞,获得库币和荣誉,厂商从这里发布众测.获取漏洞报告和修复建议. 在2017年3月份之前,补天的厂商域名URL是非 ...
- uva11636-Hello World!
题意:已知有一行printf(“Hello World!”\n);以后就可以复制,问多少次之后就可以达到所需要的行数. 题解:模拟: #include<iostream> #include ...
- 驱动调试-根据oops定位错误代码行
1.当驱动有误时,比如,访问的内存地址是非法的,便会打印一大串的oops出来 1.1以LED驱动为例 将open()函数里的ioremap()屏蔽掉,直接使用物理地址的GPIOF,如下图所示: 1.2 ...
- SolrJ 复杂查询 高亮显示
SolrJ 复杂查询 高亮显示 上一章搭建了Solr服务器和导入了商品数据,本章通过SolrJ去学习Solr在企业中的运用.笔者最先是通过公司的云客服系统接触的Solr,几百万的留言秒秒钟就查询并高亮 ...
- JAVA技术图谱
- 双向bfs-八数码问题
[八数码][1] [1]: https://www.luogu.org/problem/show?pid=1379 其实除了搜索恶心一点,好像也没什么提高+的 bfs搜的是状态. 双向bfs同时从起点 ...
- Mecanim动画系统
序言:Mecanim动画系统是Unity4.0之后退出的新版动画系统,非常适合人类动画系统.本文是作为自己的学习来讲解的, 可能会有些啰嗦,但尽量把自己的坑都为大家列出来,让大家理解透彻. 一.文件的 ...
- 使用TensorFlow实现DNN
这一节使用TF实现一个多层神经网络模型来对MNIST数据集进行分类,这里我们设计一个含有两个隐藏层的神经网络,在输出部分使用softmax对结果进行预测. 使用高级API实现多层神经网络 这里我们使用 ...
- OpenXml读取word内容(一)
OpenXml读取word内容注意事项 1.使用OpenXml读取word内容,word后缀必须是".docx":如果word后缀是".doc"需要转成&quo ...