【一天一道LeetCode】#34. Search for a Range
一天一道LeetCode系列
(一)题目
Given a sorted array of integers, 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].
(二)解题
/*
首先二分法查找目标值
然后沿着目标值左右延伸,依次找到相同值得左右边界
*/
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
int i = 0;
int j = nums.size()-1;
int idx = -1;
vector<int> ret;
while(i <= j)
{
int mid = (i+j)/2;
if(nums[mid] == target)
{
idx = mid;
break;
}
else if(nums[mid]>target)
{
j = mid-1;
}
else if(nums[mid]<target)
{
i = mid+1;
}
}
if(idx!=-1){
int k = idx;
while(k>=0 && nums[k] == target) k--;
int m = idx;
while(m<nums.size()&&nums[m] == target) m++;
ret.push_back(k+1);
ret.push_back(m-1);
}
else{
ret.push_back(-1);
ret.push_back(-1);
}
return ret;
}
};
【一天一道LeetCode】#34. Search for a Range的更多相关文章
- [array] leetcode - 34. Search for a Range - Medium
leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...
- [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 (有序数组中查找给定数字的起止下标)
题目链接: https://leetcode.com/problems/search-for-a-range/?tab=Description Problem: 在已知递减排序的数组中,查找到给定 ...
- [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][Python]34: Search for a Range
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 34: Search for a Rangehttps://oj.leetco ...
随机推荐
- 序列化战争:主流序列化框架Benchmark
序列化战争:主流序列化框架Benchmark GitHub上有这样一个关于序列化的Benchmark,被好多文章引用.但这个项目考虑到完整性,代码有些复杂.为了个人学习,自己实现了个简单的Benchm ...
- RxJava(十)switchIfEmpty操作符实现Android检查本地缓存逻辑判断
欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/52585912 本文出自:[余志强的博客] switchIfEmpty ...
- 剑指Offer——记中国银行体检之旅
剑指Offer--记中国银行体检之旅 11.23完成中国银行面试,当日回到学校.当天晚上8:39收到体检通知,自己真是又气又高兴啊.气的是自己刚从北京回来,接着又要去一次.高兴的是自己通过了面试. ...
- 给定一个实数数组,按序排列(从小到大),从数组从找出若干个数,使得这若干个数的和与M最为接近,描述一个算法,并给出算法的复杂度。
有N个正实数(注意是实数,大小升序排列) x1 , x2 ... xN,另有一个实数M. 需要选出若干个x,使这几个x的和与 M 最接近. 请描述实现算法,并指出算法复杂度. #define M 8 ...
- 使用Apache的ab进行压力测试
概述 ab是apache自带的压力测试工具,当安装完apache的时候,就可以在bin下面找到ab然后进行apache 负载压力测试. 后台测试开发中,常用的压力测试服务,php一般选择xampp,下 ...
- 剑指offer面试题6 重建二叉树(c)
- sql中InnoDB和MyISAM的区别
InnoDB和MyISAM是许多人在使用MySQL时最常用的两个表类型 1,MyISAM类型的表强调的是性能,其执行数度比InnoDB类型更快,但是不提供事务支持等高级处理,往往被认为只适合小项目:而 ...
- Android初级教程:屏幕分辨率
在app编码中经常需要获取手机的屏幕分辨率(宽*高),原来我直接上网拷贝代码,但在使用过程中却发现诸多不便. 不便一:下面代码中的getWidth和getHeight在adt上提示deprecated ...
- MyBatis Generator For Eclipse 插件安装
由于在ORM框架MyBatis中,实现数据表于JavaBean映射时,配置的代码比较的复杂,所以为了加快开发的效率,MyBatis官方提供了一个Eclipse的插件, 我izuoyongjiushis ...
- UNIX网络编程——ICMP报文分析:端口不可达
ICMP的一个规则是,ICMP差错报文必须包括生成该差错报文的数据报IP首部(包含任何选项),还必须至少包括跟在该IP首部后面的前8个字节(包含源端口和目的端口).在我们的例子中,跟在IP首部后面的前 ...