题目

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. jmeter远程运行GUI多用户负载

    1.在Jmeter控制机的bin目录下找到jmeter.properties文件并修改”remote_hosts”,增加负载机IP,多个IP使用英文逗号隔开,修改后要重启Jmeter.如下图: 2.添 ...

  2. 给Clouderamanager集群里安装基于Hive的大数据实时分析查询引擎工具Impala步骤(图文详解)

    这个很简单,在集群机器里,选择就是了,本来自带就有Impala的. 扩展博客 给Ambari集群里安装基于Hive的大数据实时分析查询引擎工具Impala步骤(图文详解)

  3. java hashCode 作用

    hashCode 作用,对象根据hashCode的值分区域存放 /** * hashCode 作用 * * @author Administrator * */ public class Point ...

  4. UvaLive6441(期望概率dp)

    1.涉及负数时同时维护最大和最小,互相转移. 2.考场上最大最小混搭转移WA,赛后发现如果是小的搭小的,大的搭大的就可过,类似这种: db a = (C[i] - W[i]) * dp1[i - ][ ...

  5. Jamie and Binary Sequence (changed after round) - CodeForces 916B

    http://codeforces.com/problemset/problem/916/B 好尬啊... #include<cstdio> #include<algorithm&g ...

  6. 数学 2015百度之星初赛2 HDOJ 5255 魔法因子

    题目传送门 /* 数学:不会写,学习一下这种解题方式:) 思路:设符合条件的数的最高位是h,最低位是l,中间不变的部分为mid,由题意可得到下面的公式(这里对X乘上1e6用a表示,b表示1e6) (h ...

  7. 学习JavaScript数据结构与算法 (一)

    学习JavaScript数据结构与算法 的笔记, 包含一二三章 01基础 循环 斐波那契数列 var fibonaci = [1,1] for (var i = 2; i< 20;i++) { ...

  8. 阻止默认行为是配合passive使用

    在使用lighthouse检测pwa应用时,发现提示下面有下面的警告 默认使用passive:true提高滚动性能并减少崩溃,passive即顺从的,是指它顺从浏览器的默认行为.设置该属性的目的主要是 ...

  9. jmeter(十八)属性和变量

    一.Jmeter中的属性: 1.JMeter属性统一定义在jmeter.properties文件中,我们可以在该文件中添加自定义的属性 2.JMeter属性在测试脚本的任何地方都是可见的(全局),通常 ...

  10. 二分图匹配 + 构造 E. Arpa’s overnight party and Mehrdad’s silent entering

    http://codeforces.com/contest/742/problem/E 跪着看题解后才会的. 对于任何一对BF[i]和GF[i] 连接了一条边后,那么他们和隔壁都是不会有边相连的了,这 ...