leetcode题解: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]
.
- 说明:
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 (已排序数组范围查找)的更多相关文章
- [LeetCode] 034. Search for a Range (Medium) (C++/Java)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
- [array] leetcode - 34. Search for a Range - Medium
leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...
- 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 ...
- LeetCode第[88]题(Java):Merge Sorted Array(合并已排序数组)
题目:合并已排序数组 难度:Easy 题目内容: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as ...
- [LeetCode每日一题]81. 搜索旋转排序数组 II
[LeetCode每日一题]81. 搜索旋转排序数组 II 问题 已知存在一个按非降序排列的整数数组 nums ,数组中的值不必互不相同. 在传递给函数之前,nums 在预先未知的某个下标 k(0 & ...
- Leetcode算法【34在排序数组中查找元素】
在之前ARTS打卡中,我每次都把算法.英文文档.技巧都写在一个文章里,这样对我的帮助是挺大的,但是可能给读者来说,一下子有这么多的输入,还是需要长时间的消化. 那我现在改变下方式,将每一个模块细分化, ...
- [LeetCode]面试题53 - I. 在排序数组中查找数字 I(二分);面试题53 - II. 0~n-1中缺失的数字(二分)
##面试题53 - I. 在排序数组中查找数字 I ###题目 统计一个数字在排序数组中出现的次数. 示例 1: 输入: nums = [5,7,7,8,8,10], target = 8 输出: 2 ...
- [LeetCode每日一题]153.寻找旋转排序数组中的最小值
[LeetCode每日一题]153.寻找旋转排序数组中的最小值 问题 已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转 后,得到输入数组.例如,原数组 nums = [0,1, ...
- C++版 - 剑指offer面试题38:数字在已排序数组中出现的次数
数字在已排序数组中出现的次数 提交网址: http://www.nowcoder.com/practice/70610bf967994b22bb1c26f9ae901fa2?tpId=13&t ...
随机推荐
- HDU - 2814 Visible Trees
题意: m*n(1<=m,n<=100000)的森林里,起始点在(1,1),某人从(0,0)点开始看,问能看到多少棵树. 题解: 求出1~x中的每个数与1~y的数中互质的数的总和.用素数筛 ...
- POJ 2891 Strange Way to Express Integers | exGcd解同余方程组
题面就是让你解同余方程组(模数不互质) 题解: 先考虑一下两个方程 x=r1 mod(m1) x=r2 mod (m2) 去掉mod x=r1+m1y1 ......1 x=r2+m2y2 . ...
- 交叉编译HTOP并移植到ARM嵌入式Linux系统
原创作品,允许转载,转载时请务必以超链接形式标明文章.作者信息和本声明,否则将追究法律责任. 最近一直在完善基于Busybox做的ARM Linux的根文件系统,由于busybox是一个精简的指令集组 ...
- Codeforces Round #504:D. Array Restoration
D. Array Restoration 题目链接:https://codeforces.com/contest/1023/problem/D 题意: 给出一个序列,现在要求对一个全为0的序列执行q次 ...
- Numpy基本数据结构
Numpy数组是一个多维数组对象,称为ndarray.其由两部分组成: 1 实际的数据 2 描述这些数据的元数据 一 ndarray的方法 # 多维数组ndarray import numpy as ...
- tips 前端 背景与元素的透明和模糊
碰到好几次这样的情况了: 一个带点儿文艺效果 背景图片模糊 而一行别致的文字清晰的悬浮在背景上(口胡,加点美好的想象,生活会更美好) 第一反应是 this is easy. cause i know ...
- 行为型设计模式之备忘录模式(Memento)
结构 意图 在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存的状态. 适用性 必须保存一个对象在某一个时刻的(部分)状态, 这样以后需要时 ...
- lightgbm 学习笔记
首先是配置文件参数.参考自https://lightgbm.apachecn.org/#/docs/6 配置参数实在是太多了,大家还是去原文档查表吧orz 参数名 可选参数 作用 config= 自填 ...
- python任意编码转utf8或者unicode
# encoding: utf-8 ''' Created on 2015年2月8日 @author: 张鹏程 aprial@163.com @copyright: 版权所有, 尊重劳动成功, 转载与 ...
- What does this bit-manipulating function do?
http://stackoverflow.com/questions/8637142/what-does-this-bit-manipulating-function-do unsigned long ...