题目

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;
}
};

GitHub测试程序源码

LeetCode(34)Search for a Range的更多相关文章

  1. 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 ...

  2. LeetCode(81) Search in Rotated Array II

    题目 Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would t ...

  3. LeetCode(34):搜索范围

    Medium! 题目描述: 给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n) 级别. 如果 ...

  4. 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 ...

  5. 【LeetCode OJ 34】Search for a Range

    题目链接:https://leetcode.com/problems/search-for-a-range/ 题目:Given a sorted array of integers, find the ...

  6. LeetCode(35) Search Insert Position

    题目 Given a sorted array and a target value, return the index if the target is found. If not, return ...

  7. Leetcode(1)两数之和

    Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...

  8. Leetcode(3)无重复字符的最长子串

    Leetcode(3)无重复字符的最长子串 [题目表述]: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 第一种方法:暴力 执行用时:996 ms: 内存消耗:12.9MB 效果: ...

  9. Leetcode(5)最长回文子串

    Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...

随机推荐

  1. AdventureWorks2012.mdf的使用

    AdventureWorks2012.mdf的使用,在数据库管理器界面中,右击数据库,然后附加,然后选择好AdventureWorks2012.mdf,然后删掉log,然后确定即可.

  2. jvm 实战

    https://blog.csdn.net/neutrojan/article/details/50532590# 1.ps -ef |grep java 找出最耗性能的JAVA进程2.top -Hp ...

  3. Python之单元测试——HTMLTestRunner

    前置条件:把HTMLTestRunner.py文件拷贝到External Libraries—>site-packages里面 import unittestimport HTMLTestRun ...

  4. 通过流传入excel解析的问题

    做了个excel文件导入的功能,接收excel文件流,先读取文件流头部信息确定文件类型,然后调用poi方法 OPCPackage pkg = OPCPackage.open(inputStream) ...

  5. 一些关于Spring的随笔

    Spring的IOC.AOP IOC(Inversion of Control): spring容器控制了所有的bean,不用spring以前,一个bean要依赖另一个bean就在这个bean里初始化 ...

  6. UVM挑战及概述

    UVM的调度也具有其独特的挑战,尤其是在调试的领域.其中的一些挑战如下: 1. Phase的管理:objections and synchronization 2. 线程调试 3. Tracing i ...

  7. life of a NPTL pthread

    这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=91 March 7, 2013 life of a NPTL pthread ...

  8. 如何处理Docker的错误消息request canceled:Docker代理问题

    在本地安装Kubernetes时,遇到错误消息: request canceled while waiting for connection(Client.Timeout exceeded while ...

  9. javaee 第六周作业

    一.jsf(java server faces)的运行原理(工作方式) 1.jsf应用是事件驱动的,当一个事件发生时(比如用户单击一个按钮),事件通知通过HTTP发往服务器,服务器端使用叫做Faces ...

  10. 2015Java参赛邀请函

    [导读]甲骨文公司值Java语言发布20周年之际,携手全国高等级学校计算机教育研究会.教育部高等学校计算机类专业教学指导委员会,共同举办了2015年“甲骨文杯”全国Java程序设计大赛,为二百万名中国 ...