leetcode——Search for a Range 排序数组中寻找目标下标范围(AC)
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].
class Solution {
public:
vector<int> searchRange(int A[], int n, int target) {
assert(A!=NULL && n!=0);
vector<int> result;
int start = 0, end = n-1, mid;
while(start <= end)
{
mid = (start+end)/2;
if(A[mid] == target)
{
while(mid>=0 && A[mid]==target)
mid--;
result.push_back(++mid);
while(mid<=n-1 && A[mid]==target)
mid++;
result.push_back(--mid);
return result;
}
else if(A[mid] < target)
start = mid+1;
else
end = mid-1;
}
result.push_back(-1);
result.push_back(-1);
return result;
}
};leetcode——Search for a Range 排序数组中寻找目标下标范围(AC)的更多相关文章
- Leetcode算法【34在排序数组中查找元素】
在之前ARTS打卡中,我每次都把算法.英文文档.技巧都写在一个文章里,这样对我的帮助是挺大的,但是可能给读者来说,一下子有这么多的输入,还是需要长时间的消化. 那我现在改变下方式,将每一个模块细分化, ...
- LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)
题目链接: https://leetcode.com/problems/search-for-a-range/?tab=Description Problem: 在已知递减排序的数组中,查找到给定 ...
- Leetcode题库——26.删除排序数组中的重复项
@author: ZZQ @software: PyCharm @file: removeDuplicates.py @time: 2018/9/23 13:51 要求: 给定一个排序数组,你需要在原 ...
- LeetCode刷题--26.删除排序数组中的重复项(简单)
题目描述 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度.不要使用额外的数组空间,你必须在原地修改输入数组并在使用O(1)额外空间的条件下完成. 示例 ...
- leetcode刷题-80.删除排序数组中的重复项 II
题目 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. ...
- Leetcode(26)-删除排序数组中的重复项
给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. 我们利用 ...
- leetcode:Search for a Range(数组,二分查找)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- C#LeetCode刷题之#34-在排序数组中查找元素的第一个和最后一个位置(Find First and Last Position of Element in Sorted Array)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4970 访问. 给定一个按照升序排列的整数数组 nums,和一个目 ...
- leetcode 26 80 删除已排序数组中重复的数据
80. Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if dupli ...
随机推荐
- jQuery-处理元素内容、表单元素
处理元素内容 1.text方法 使用说明: 1)不传参数 得到jQuery对象内所有元素及其后代元素的文本内容 2)传入用于设置匹配元素的文本内容 3)传入function 使用函数来设置jQuery ...
- Struts2 ajax json使用介绍
一.jar包首先引入Struts和json所需的jar包. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaXRteWhvbWUxOTkw/font/5a6 ...
- SQL Server 备份和还原数据库
备份: --完整备份 ) set @db_name = 'WSS_Content_Test'; ) set @db_location = 'D:\spbr0002\0000000B.bak'; --保 ...
- Linux安装php运行环境
安装apache: yum install httpd httpd-devel 启动apache: /etc/init.d/httpd start 此时输入服务器的IP地址,应该看到apache的服 ...
- WPF 自定义命令 以及 命令的启用与禁用
自定义命令: 在WPF中有5个命令类(ApplicationCommands.NavigationCommands.EditingCommands.ComponentCommands 以及 M ...
- POJ 1273 Drainage Ditches (网络最大流)
http://poj.org/problem? id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- ios iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)
iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势) 转自容芳志大神的博客:http://www.cnblogs.com/stoic/archive/2013/02/27/2940 ...
- 第三篇:关于TIME_WAIT状态
前言 为何TCP ”四次分手“ 的过程中会有一个TIME_WAIT状态?这个状态有什么意义呢?这是网络中的一个经典问题,本文将给出精简的回答. 什么是TIME_WAIT状态 这是TCP通信协议中出现的 ...
- View的setTag()与getTag()方法使用
通常我们是用findViewById()方法来取得我们要使用的View控件,不过除了这一种方法之处 ,我们还可以用View中的setTag(Onbect)给View添加一个格外的数据,再用getTag ...
- STM32总线结构和存储器
也就说我们所操作的就是别名区的位