• 题目:

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. P1023 税收与补贴问题

    题目背景 每样商品的价格越低,其销量就会相应增大.现已知某种商品的成本及其在若干价位上的销量(产品不会低于成本销售),并假设相邻价位间销量的变化是线性的且在价格高于给定的最高价位后,销量以某固定数值递 ...

  2. 关于js的addEventListener 和一些常用事件

    element.addEventListener(<event-name>, <callback>, <use-capture>);document.addEven ...

  3. freescale 16位单片机的地址映射

    以MC9S12XS128MAL为例,其实DG128之类的类似.如图一,128代表的是单片机中的FLASH大小为128K Byte,同理64代表的是单片机中的FLASH大小为64 K Byte,256代 ...

  4. [ CodeVS冲杯之路 ] P1010

    不充钱,你怎么AC? 题目:http://codevs.cn/problem/1010/ 首先我们将坐标都+1,因为它是从(0,0)开始的 预处理出禁区,也就是马能到达的格子和马自己的格子,标上记号 ...

  5. [HDU_3652]B-number

    题目描述 A wqb-number, or B-number for short, is a non-negative integer whose decimal form contains the ...

  6. zmap zgrab 环境搭建

    yum install cmake gmp-devel gengetopt libpcap-devel flex byacc json-c-devel libunistring-devel golan ...

  7. linux select函数详解【转】

    转自:http://www.cnblogs.com/ccsccs/articles/4224253.html 在Linux中,我们可以使用select函数实现I/O端口的复用,传递给 select函数 ...

  8. springMVC 配置中易犯的小错误

    搭建springMVC环境时有可能遇到:'警告: No mapping found for HTTP request with URI [/WEB-INF/pages/helloWorld.jsp] ...

  9. JAVA泛型练手

    公司电脑不能安装JAVA环境,不爽啊. import java.util.List; import java.util.ArrayList; import java.lang.reflect.Meth ...

  10. PyCharm配置gitHub远程仓储

    在一个团队里,编码不能是闭门造车,git学起来: 1. GIT的基本介绍.安装及使用教程- @廖雪峰 2. pycharm配置github远程仓储- @谢小小XH