[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: 在已知递减排序的数组中,查找到给定 ...
随机推荐
- TensorFlow简易学习[2]:实现线性回归
上篇介绍了TensorFlow基本概念和基本操作,本文将利用TensorFlow举例实现线性回归模型过程. 线性回归算法 线性回归算法是机器学习中典型监督学习算法,不同于分类算法,线性回归的输出是整个 ...
- Python之文件的基本操作
在python中,对文件的基本操作一共有如下四种: 1.打开文件 file_obj = open("文件路径","模式") 常用的打开文件模式有: r:以只读方 ...
- ionic3中 ion-datetime 全屏可点击问题解决方案
废话不多说,能进来的都应该知道是个什么情况.我也是在网上找了一段时间,才在git上ionic官方团队的Issues中找到了问题解决方法. 第一,给外围包上一层ion-item,但是这有个问题,就是会让 ...
- Linux系统bash shell之历史命令
1.相关变量: HISTSIZE: 定义命令历史记录的条数 HISTFILE: 定义命令储存的文件,一般是 ~/.bash_history HISTFILESIZE: 定义了历史文件记录历史的条数 H ...
- js限制日期选择范围是两个月
$(".dateInputClass input:eq(0)").bind("click", function(){WdatePicker({dateFmt:' ...
- gdb的多线程调试
info threads 可以查看当前进程有哪些线程 thread ID 可以切换到线程ID bt 查看当前线程堆栈 set scheduler-locking on多线程调试过程中, 线程会来回切换 ...
- 用python 抓取B站视频评论,制作词云
python 作为爬虫利器,与其有很多强大的第三方库是分不开的,今天说的爬取B站的视频评论,其实重点在分析得到的评论化作嵌套的字典,在其中取出想要的内容.层层嵌套,眼花缭乱,分析时应细致!步骤分为以下 ...
- svn搭建文档
1.制作本地yum源 a)挂载光盘 [root@localhost ~]# mount /dev/cdrom /mnt b)删除/etc/yum.repos.d目录所有的repo文件 [root@ ...
- appium测试准备记录
一 获取应用程序包名(手机中不安装apk) windows 环境下: aapt工具 使用aapt工具,适合给程序自动获取apk的相关信息. //aapt 是sdk自带的一个工具,在SDK/buildt ...
- 栈和队列的java简单实现
今天看了一本书<啊哈 算法>,书的内容不多,一共两章,第一章是常见的排序算法包括桶排序.冒泡排序和快速排序,这些事基础的排序算法网上有很多资料说明,这里主要说第二章栈,对列,链表,书上使用 ...