【题目】
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 ofO(logn).
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),空间复杂度是O(1)
具体可以参考:[经典面试题]二分查找问题汇总
【代码】
/*********************************
* 日期:2015-01-24
* 作者:SJF0115
* 题目: 34.Search for a Range
* 网址:https://oj.leetcode.com/problems/search-for-a-range/
* 结果:AC
* 来源:LeetCode
* 博客:
**********************************/
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> searchRange(int A[], int n, int target) {
vector<int> result;
if(n <= 0){
return result;
}//if
// 目标元素的最小位置
int left = searchStartRange(A,n,target);
// 目标元素的最大位置
int right = searchEndRange(A,n,target);
result.push_back(left);
result.push_back(right);
return result;
}
private:
// 目标元素的最小位置
int searchStartRange(int A[],int n,int target){
int start = 0,end = n-1;
while(start <= end){
int mid = (start + end) / 2;
// 目标是中间元素
if(A[mid] == target){
// 如果中间元素左边元素等于目标元素
if(mid - 1 >= 0 && A[mid - 1] == target){
end = mid - 1;
}//if
else{
return mid;
}
}
// 目标位于右半部分
else if(A[mid] < target){
start = mid + 1;
}//
// 目标位于左半部分
else{
end = mid - 1;
}
}//while
return -1;
}
// 目标元素的最大位置
int searchEndRange(int A[],int n,int target){
int start = 0,end = n-1;
while(start <= end){
int mid = (start + end) / 2;
// 目标是中间元素
if(A[mid] == target){
// 如果中间元素右边元素等于目标元素
if(mid + 1 < n && A[mid + 1] == target){
start = mid + 1;
}//if
else{
return mid;
}
}
// 目标位于右半部分
else if(A[mid] < target){
start = mid + 1;
}//
// 目标位于左半部分
else{
end = mid - 1;
}
}//while
return -1;
}
};
int main(){
Solution solution;
int A[] = {1};
int n = 1;
int target = 0;
vector<int> result = solution.searchRange(A,n,target);
// 输出
for(int i = 0;i < result.size();++i){
cout<<result[i]<<endl;
}//for
return 0;
}
【代码二】
class Solution {
public:
vector<int> searchRange(int A[], int n, int target) {
vector<int> result;
if(n <= 0){
return result;
}//if
// 目标元素的最小位置
int left = searchStartRange(A,n,target);
// 目标元素的最大位置
int right = searchEndRange(A,n,target);
result.push_back(left);
result.push_back(right);
return result;
}
private:
// 目标元素的最小位置
int searchStartRange(int A[],int n,int target){
int start = 0,end = n-1;
while(start <= end){
int mid = (start + end) / 2;
// 目标位于右半部分
if(A[mid] < target){
start = mid + 1;
}
// 目标位于左半部分
else{
end = mid - 1;
}
}//while
// 目标不存在的情况
// 此时start = end
if(A[start] != target){
return -1;
}//if
else{
return start;
}
}
// 目标元素的最大位置
int searchEndRange(int A[],int n,int target){
int start = 0,end = n-1;
while(start <= end){
int mid = (start + end) / 2;
// 目标位于右半部分
if(A[mid] > target){
end = mid - 1;
}//
// 目标位于左半部分
else{
start = mid + 1;
}
}//while
// 目标不存在的情况
// 此时start = end
if(A[end] != target){
return -1;
}//if
else{
return end;
}
}
};
分享到:
相关推荐
用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现...
JVM 基础 JAVA 并发 JVM 性能调优 LeetCode 算法 .......
Leetcode 题解.pdf
My Solutions to Leetcode Database problems. 我的 Leetcode 数据.zip
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101.zip
LeetCode 后端.zip
Leetcode101.zip
原创:leetcode 111. 二叉树的最小深度记住:最小深度和最大深度方法不同。* Definition for a binary tree node.in
原创:leetcode 107. 二叉树的层次遍历 II【队列】* Definition for a binary tree node.
刷leetcode总结.md
Recording personal Java, Python, JavaScript solutions for Leetcode problems. 记录个人 Java, Python, JavaScript 的Leetcode题解.zip
原创:leetcode 5. 最长回文子串//寻找以i-1,i为中点偶数长度的回文//寻找以i为中心的奇数长度的回文。
My Solutions to Leetcode problems. All solutions support C
leetcode-editor,在ide中做leetcode练习,支持leetcode.com和leetcode-cn.com,以满足练习的基本需求。理论上支持:intellij idea phpstorm webstorm pycharm rubymine appcode clion goland datagrip rider mps ...
原创:leetcode 22.括号生成【回溯】对待这种问题,千万别暴力搜索,那样太笨了。但是这个方法是最容易理解的//回溯法 (后面的括号) 不可以大于 (前面
LeetCode674. 最长连续递增序列674. 最长连续递增序列解题思路:记录每次递增序列的长度,max存储最大长度// 递增序列更新最大长度} else
LeetCode888. 公平的糖果棒交换888. 公平的糖果棒交换解题思路:哈希表法第一次遍历 A\B 计算二者的糖果差值 diff,同时以 B糖果值建立哈希
LeetCode746.使用最小花费爬楼梯746. 使用最小花费爬楼梯解题思路:动态规划当前楼梯最小值=Math.min(前一步最小值,前两步最小值)简化 mi