• 题目:

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

  • 说明:

              1)已排序数组查找,二分查找

  • 实现:

  1. STL实现
 class Solution {
public:
vector<int> searchRange(int A[], int n, int target) {
auto low_bound=lower_bound(A,A+n,target);//第一个大于等于>=target元素的指针位置
auto up_bound=upper_bound(low_bound,A+n,target);//第一个大于>target元素的指针位置
if(*low_bound==target)//target是否存在于A[]
{
return vector<int>{distance(A,low_bound),distance(A,prev(up_bound))};
}
else return vector<int>{-,-}; }
};

     2.  常规实现

 class Solution {
public:
vector<int> searchRange(int A[], int n, int target) {
int low=,high=n-,middle;
bool isFind=false;
vector<int> vec;
while(low<=high)//二分查找,直至找到,并置标志true
{
middle=(low+high)/;
if(A[middle]==target)
{
isFind=true;
break;
}
else if(A[middle]<target)
low=middle+;
else
high=middle-;
}
if(isFind)//如果找到,确定开始、结束与target相等的元素位置
{
low=middle;
high=middle;
while(low>=&&A[low]==target) low--;//下界要>=0
low++;
while(high<=n-&&A[high]==target) high++;//上界要<=n-1
high--;
vec.push_back(low);
vec.push_back(high);
}
else//没有目标值
{
vec.push_back(-);
vec.push_back(-);
}
return vec;
}
};

leetcode题解:Search for a Range (已排序数组范围查找)的更多相关文章

  1. [LeetCode] 034. Search for a Range (Medium) (C++/Java)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...

  2. [array] leetcode - 34. Search for a Range - Medium

    leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...

  3. LeetCode第[4]题(Java):Median of Two Sorted Arrays (俩已排序数组求中位数)——HARD

    题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...

  4. LeetCode第[88]题(Java):Merge Sorted Array(合并已排序数组)

    题目:合并已排序数组 难度:Easy 题目内容: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as ...

  5. [LeetCode每日一题]81. 搜索旋转排序数组 II

    [LeetCode每日一题]81. 搜索旋转排序数组 II 问题 已知存在一个按非降序排列的整数数组 nums ,数组中的值不必互不相同. 在传递给函数之前,nums 在预先未知的某个下标 k(0 & ...

  6. Leetcode算法【34在排序数组中查找元素】

    在之前ARTS打卡中,我每次都把算法.英文文档.技巧都写在一个文章里,这样对我的帮助是挺大的,但是可能给读者来说,一下子有这么多的输入,还是需要长时间的消化. 那我现在改变下方式,将每一个模块细分化, ...

  7. [LeetCode]面试题53 - I. 在排序数组中查找数字 I(二分);面试题53 - II. 0~n-1中缺失的数字(二分)

    ##面试题53 - I. 在排序数组中查找数字 I ###题目 统计一个数字在排序数组中出现的次数. 示例 1: 输入: nums = [5,7,7,8,8,10], target = 8 输出: 2 ...

  8. [LeetCode每日一题]153.寻找旋转排序数组中的最小值

    [LeetCode每日一题]153.寻找旋转排序数组中的最小值 问题 已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转 后,得到输入数组.例如,原数组 nums = [0,1, ...

  9. C++版 - 剑指offer面试题38:数字在已排序数组中出现的次数

    数字在已排序数组中出现的次数 提交网址: http://www.nowcoder.com/practice/70610bf967994b22bb1c26f9ae901fa2?tpId=13&t ...

随机推荐

  1. winform中key读取修改

    根据key name的名称读取value-----读取使用ConfigurationManager.AppSettings读取容易没读取到根目录中的key public string GetXml(s ...

  2. mysql慢查询工具

    GeorgeHao 安装过程: [root@localhost-centos6 ~]# wget percona.com/get/pt-query-digest [root@localhost-cen ...

  3. 修改Eclipse发布路劲(Deploy path)

    Eclipse默认的发布路劲在用户的workspace里,比较难找,为了方便查看,可以将发布路劲修改为自己想要的. 修改方法: 1.先stop Servers下tomcat里的项目,然后remove ...

  4. linux下搭建SVN服务器完全手册【转】

    转自:http://blog.csdn.net/bullbat/article/details/9115559 系统环境        RHEL5.4最小化安装(关iptables,关selinux) ...

  5. blog postman 翻译(2):postman中新增request attributes 的描述(desc)

    blog的原文链接是:http://blog.getpostman.com/2017/06/03/descriptions-for-request-attributes-in-postman/ pos ...

  6. ppt提取图片,视频文件

    1,将视频另存为htm格式,然后会生成文件夹,里面就有. 2,如果没法生成htm格式,可以生成odp格式,压缩文件,用解压缩软件查看odp文件,就能看到. 3,pptx直接修改后缀为rar或者zip, ...

  7. Ionic 存储目录 CORS

    使用不同的存储库结构官方为 { "scripts": { "install": "cd path-to/your-app && npm ...

  8. 本地添加maven支持

    第一步:添加maven支持,去Apache maven官网下载maven, 解压 在conf文件夹里有个settings.xml,这个是需要自己配置的,不然的话也会有默认,只是那样的话会在C盘了,不喜 ...

  9. (4)C#工具箱-菜单和工具栏

    1.ContextMenuStrip(右键菜单栏) 把contextMenuStrip控件拖到窗体上,会在窗体下面显示,点击控件在最上行显示菜单栏,可以任意设置.(运行以后不会在界面上显示,它需要预控 ...

  10. Codeforces Round #262 (Div. 2) A. Vasya and Socks【暴力/模拟/袜子在可以在合法情况下增加后用几天】

    A. Vasya and Socks time limit per test 1 second memory limit per test 256 megabytes input standard i ...