LeetCode(34)Search for a Range
题目
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].
分析
给定一有序整数序列,与目标元素值,要求输出目标元素值在此序列中出现的范围下标。且复杂度控制在O(logn)内。
明显的,我们应该采取二分搜索的思想,设计求出关键字最早出现位置与最后出现位置,与普通的二叉搜索比较,只需要修改判断条件即可。
AC代码
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> ret;
if (nums.size() == 0)
{
ret.push_back(-1);
ret.push_back(-1);
return ret;
}
//寻找目标元素的下标
int pos = BinarySearch(nums, target);
//目标元素不存在
if (pos == -1)
{
ret.push_back(-1);
ret.push_back(-1);
return ret;
}
else{
int left = BinarySearchLeft(nums, 0, pos, target);
int right = BinarySearchRight(nums, pos, nums.size()-1 , target);
ret.push_back(left);
ret.push_back(right);
return ret;
}//if
}
int BinarySearch(vector<int> & nums, int target)
{
int left = 0, right = nums.size() - 1;
while (left <= right)
{
int mid = (left + right) / 2;
if (nums[mid] == target)
return mid;
else if (nums[mid] < target)
{
left = mid + 1;
continue;
}
else{
right = mid - 1;
continue;
}
}//while
return -1;
}
int BinarySearchLeft(vector<int> & nums, int left, int right, int target)
{
while (left < right)
{
int mid = (left + right) / 2;
if (nums[mid] == target && nums[mid-1] < target)
return mid;
else if (nums[mid] < target)
{
left = mid + 1;
continue;
}
else{
right = mid - 1;
continue;
}
}//while
return left;
}
int BinarySearchRight(vector<int> & nums, int left, int right, int target)
{
while (left < right)
{
int mid = (left + right) / 2;
if (nums[mid] == target && nums[mid + 1] > target)
return mid;
else if (nums[mid] <= target)
{
left = mid + 1;
continue;
}
else{
right = mid - 1;
continue;
}
}//while
return right;
}
};
LeetCode(34)Search for a Range的更多相关文章
- LeetCode(74) Search a 2D Matrix
题目 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the fo ...
- LeetCode(81) Search in Rotated Array II
题目 Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would t ...
- LeetCode(34):搜索范围
Medium! 题目描述: 给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n) 级别. 如果 ...
- LeetCode(33)Search in Rotated Sorted Array
题目 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 m ...
- 【LeetCode OJ 34】Search for a Range
题目链接:https://leetcode.com/problems/search-for-a-range/ 题目:Given a sorted array of integers, find the ...
- LeetCode(35) Search Insert Position
题目 Given a sorted array and a target value, return the index if the target is found. If not, return ...
- Leetcode(1)两数之和
Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...
- Leetcode(3)无重复字符的最长子串
Leetcode(3)无重复字符的最长子串 [题目表述]: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 第一种方法:暴力 执行用时:996 ms: 内存消耗:12.9MB 效果: ...
- Leetcode(5)最长回文子串
Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...
随机推荐
- OpenCv图像像素操作
1:像素 有两种直接操作像素点的方法: 第一种: 将其转化为numpy.array格式,直接进行操作. 第二种:使用Opencv提供的Get1D,Get2D等函数. 2:获取行和列像素 有一下四个函数 ...
- 449B
B. Chtholly's request time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- Python基础知识(2)
1:if比较运算符.and.or >=:大于或者等于 <=:小于或者等于 ==:等于 !=:不等于 (<>:也是不等于,在Python2中可用.Python3中无法使用) a ...
- 清除select自带小三角
select {/*Chrome和Firefox里面的边框是不一样的,所以复写了一下*/border: solid 1px #000; /*很关键:将默认的select选择框样式清除*/appeara ...
- April Fools Contest 2017 C
Description DO YOU EXPECT ME TO FIND THIS OUT? WHAT BASE AND/XOR LANGUAGE INCLUDES string? DON'T BYT ...
- FACVSPOW - Factorial vs Power 数学方法 + 二分
http://www.spoj.com/problems/FACVSPOW/ 求解n! > a^n最小的整数n 对于有n!和a^n的东西,一般是取ln 然后就是求解 (ln(1) + ln(2) ...
- 通过流传入excel解析的问题
做了个excel文件导入的功能,接收excel文件流,先读取文件流头部信息确定文件类型,然后调用poi方法 OPCPackage pkg = OPCPackage.open(inputStream) ...
- Springboot + Websocket + Sockjs + Stomp + Vue + Iview 实现java后端日志显示在前端web页面上
话不多说,看代码. 一.pom.xml 引入spring boot websocket依赖 <dependency> <groupId>org.springframework. ...
- Spring-aop(一)
写一个计算类,计算前后需要打印日志. interface ArithmeticCalculator { public int add(int i, int j); public int sub(int ...
- python实现: protobuf解释器
之前项目为了自动化,所以写一个protobuf的解释器,用来生成项目所需的格式. 当然现在通过以下链接的指导,跳过手工分析,直接生成代码了. https://developers.google.com ...