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 ...
随机推荐
- 清理SYSAUX表空间的WRH$_LATCH_CHILDREN表
周六 被突然起来的短信 轰醒. 一看有63条短信. 都是来之与监控中的.有关表空间大小超过某个警戒值. 发现 SYSAUX表空间超过了15GB 通过以下代码查看SYSAUX表空间的功能占用情况 SEL ...
- jQuery对象的链式操作用法分析
可以使用下面的原则判断一个函数返回的时候是jQuery对象,即是否可以用于链式操作. 除了获取某些数据的函数,比如获取属性值"attr(name)",获取集合大小"siz ...
- 笔记本怎么设置WIfi热点
随着手机的发展,流量的消耗也是大大地增加.虽然很多手机支持wifi,但是不加密或者知道密码的wifi热点却寥寥无几.笔记本的无线网卡显出神通了.那么,如何在笔记本上建立wifi热点呢? 工具/原料 一 ...
- 教您如何在Word的mathtype加载项中修改章节号
在MathType数学公式编辑器中,公式编号共有五部分内容:分别是章编号(Chapter Number).节编号(Section Number).公式编号(Equation Number).括号(En ...
- Cocostudio学习笔记(2) Button + CheckBox
这篇记录了两个控件的使用流程:Button 和 CheckBox. ------------------------------------------------------------------ ...
- Swift学习笔记之--类和对象
通过在 class后接类名称来创建一个类.在类里边声明属性与声明常量或者变量的方法是相同的,唯一的区别的它们在类环境下.同样的,方法和函数的声明也是相同的写法 class Shape { func s ...
- oracle启动报ORA-03113;
[案例] 在重启数据库过程中: SQL> startup ORACLE instance started. Total System Global Area 1.0489E+10 bytes F ...
- Java中自己定义缓存方式
说说自己在开发中经常用到的写法. /** * 数据缓存 * @author * */public class DataCache { /** 对象缓存*/ public static Ma ...
- [译] 关于CSS中的float和position
原文 http://learn.shayhowe.com/advanced-html-css/detailed-css-positioning 当构建页面排版时,有不同的方法可以使用.使用哪一种方法取 ...
- LeetCode——Consecutive Numbers
Description: Write a SQL query to find all numbers that appear at least three times consecutively. + ...