LeetCode OJ: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].
查找一个区间开头以及结尾的下标,分两次二分查找,一次向左一次向右即可,代码如下:
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
int sz = nums.size();
int left = bs(nums, , sz, target, true);
int right = bs(nums, , sz, target, false);
vector<int> res;
res.push_back(left);
res.push_back(right);
return res;
}
int bs(vector<int>& nums, int beg, int end, int target, int goLeft)
{
if(beg > end)
return -;
int mid = (beg + end)/;
if(nums[mid] == target){
int tmpAns = (goLeft == true ? bs(nums, beg, mid - , target, goLeft) : bs(nums, mid + , end, target, goLeft));
return tmpAns == - ? mid : tmpAns;
}else if(nums[mid] < target){
return bs(nums, mid + , end, target, goLeft);
}else{
return bs(nums, beg, mid - , target, goLeft);
}
}
};
LeetCode OJ:Search for a Range(区间查找)的更多相关文章
- [array] leetcode - 34. Search for a Range - Medium
leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...
- [OJ] Search for a Range
LintCode 61. Search for a Range (Medium) LeetCode 34. Search for a Range (Medium) class Solution { p ...
- [LeetCode] 034. Search for a Range (Medium) (C++/Java)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
- LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- [LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)
原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an ...
- leetCode 34.Search for a Range (搜索范围) 解题思路和方法
Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...
- leetcode 34 Search for a Range(二分法)
Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...
- LeetCode 034 Search for a Range
题目要求:Search for a Range Given a sorted array of integers, find the starting and ending position of a ...
- LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)
题目链接: https://leetcode.com/problems/search-for-a-range/?tab=Description Problem: 在已知递减排序的数组中,查找到给定 ...
- LeetCode Search for a Range (二分查找)
题意 Given a sorted array of integers, find the starting and ending position of a given target value. ...
随机推荐
- C# 调用win api获取chrome浏览器中地址
//FindWindow 查找窗口 //FindWindowEx查找子窗口 //EnumWindows列举屏幕上的所有顶层窗口,如果回调函数成功则返回非零,失败则返回零 //GetWindowText ...
- 吴超老师课程--Flume的安装和介绍
常用的分布式日志收集系统
- nginx_cdn配置
upstream backend { server 1.1.1.1; keepalive 128; } proxy_temp_path /dev/shm; proxy_cache_path /data ...
- go——结构
Go语言中数组可以存储同一类型的数据,但在结构体中我们可以为不同项定义不同的数据类型.结构体是由一系列具有相同类型或不同类型的数据构成的数据集合. 结构体定义需要使用type和struct语句.str ...
- 常用模块(hashlib,configparser,logging)
常用模块(hashlib,configparser,logging) hashlib hashlib 摘要算法的模块md5 sha1 sha256 sha512摘要的过程 不可逆能做的事:文件的一致性 ...
- Seaborn相关
Seaborn:基于Matplotlib,seaborn提供许多功能,比如:内置主题.颜色调色板.函数和提供可视化单变量.双变量.线性回归的工具.其能帮助我们构建复杂的可视化. ————————缩写定 ...
- 【c++习题】【17/4/13】stack
1.stack 模板.动态内存分配.析构 #include "stack2.cpp" #include <iostream> using namespace std; ...
- Django 函数和方法的区别
函数和方法的区别 1.函数要手动传self,方法不用传 2.如果是一个函数,用类名去调用,如果是一个方法,用对象去调用 class Foo(object): def __init__(self): s ...
- 20145219 《Java程序设计》实验三 敏捷开发与XP实践
20145219 <Java程序设计>实验三 敏捷开发与XP实践 实验内容 XP基础 XP核心实践 相关工具 实验步骤 敏捷开发与XP 1.敏捷开发(Agile Development)是 ...
- 在 React Native 中使用 Redux 架构
前言 Redux 架构是 Flux 架构的一个变形,相对于 Flux,Redux 的复杂性相对较低,而且最为巧妙的是 React 应用可以看成由一个根组件连接着许多大大小小的组件的应用,Redux 也 ...