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.' 第一种方法:未完成:利用回文子串的特点 ...
随机推荐
- JSR 303 - Bean Validation 模型验证
类是转载的,不知道转的哪里的. 此类依赖 JSR 303 – Bean Validation, Hibernate Validator. 代码不能直接运行.意会一下.自己改改. import com. ...
- C#程序结构与基本语法
C# 程序结构 Hello World 实例 一个 C# 程序主要包括以下部分: 命名空间声明(Namespace declaration) 一个 class Class 方法 Class 属性 一个 ...
- iOS 获取类的字符串名称 Swift4
以下实例基于Swift4,且在class, struct, enum中都可用: class Foo { // 实例属性中指定明确的类名来获取名称 var typeName: String { ...
- 洛谷P2502[HAOI2006]旅行
题目: Z小镇是一个景色宜人的地方,吸引来自各地的观光客来此旅游观光.Z小镇附近共有N个景点(编号为1,2,3,-,N),这些景点被M条道路连接着,所有道路都是双向的,两个景点之间可能有多条道路.也许 ...
- 在Android 源码中添加系统服务
Android系统本身提供了很多系统服务,如WindowManagerService,PowerManagerService等.下面描述一下添加一个系统服务的具体步骤. 1.定义自定义系统服务接口 撰 ...
- 474 Ones and Zeroes 一和零
在计算机界中,我们总是追求用有限的资源获取最大的收益.现在,假设你分别支配着 m 个 0 和 n 个 1.另外,还有一个仅包含 0 和 1 字符串的数组.你的任务是使用给定的 m 个 0 和 n 个 ...
- elasticsearch-sql安装
Github地址:https://github.com/NLPchina/elasticsearch-sql elasticsearch-sql插件可以方便我们使用SQL语言来对elasticsear ...
- Spring-aop(一)
写一个计算类,计算前后需要打印日志. interface ArithmeticCalculator { public int add(int i, int j); public int sub(int ...
- 协程和I/O模型
1.协程: 单线程实现并发 在应用程序里控制多个任务的切换+保存状态 优点: 应用程序级别速度要远远高于操作系统的切换 缺点: 多个任务一旦有一个阻塞没有切换,整个线程都阻塞在原地 该线程内的其他的任 ...
- yum install perl-ExtUtils-MakeMaker
Can't locate ExtUtils/MakeMaker.pm in @INC (@INC contains: /usr/local/lib/perl5 /usr/local/share/per ...