代码题(1)—lower_bound和upper_bound算法
1、lower_bound:查找序列中的第一个出现的值大于等于val的位置
这个序列中可能会有很多重复的元素,也可能所有的元素都相同,为了充分考虑这种边界条件,STL中的lower_bound算法总体上是才用了二分查找的方法,但是由于是查找序列中的第一个出现的值大于等于val的位置,所以算法要在二分查找的基础上做一些细微的改动。
首先是我修改数据结构课本上的二分查找实现的lower_bound算法:
int lower_bound(vector<int> array, int size, int key)
{
int low = , high = size - ;
int mid, pos = ; //需要pos记录位置
while (low < high)
{
mid = (low + high) / ;
if (array[mid] < key) //若中位数的值小于key的值,我们要在右边子序列中查找,这时候pos可能是右边子序列的第一个
{
low = mid + ;
pos = low;
}
else
{
high = mid; //若中位数的值大于等于key,我们要在左边子序列查找,但有可能middle处就是最终位置,所以我们不移动last
pos = high;
}
}
return pos;
}
2、upper_bound:返回的是最后一个大于等于val的位置,也是有一个新元素val进来时的插入位置。
int upper_bound(vector<int> array, int size, int key)
{
int low = , high = size - ;
int mid, pos = ;
while (low < high)
{
mid = (low + high) / ;
if (array[mid] <= key)
{
low = mid + ;
pos = low;
}
else
{
high = mid;
pos = high;
}
}
return pos;
}
代码题(1)—lower_bound和upper_bound算法的更多相关文章
- STL源码学习----lower_bound和upper_bound算法
转自:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html 先贴一下自己的二分代码: #include <cstdio&g ...
- STL源码学习----lower_bound和upper_bound算法[转]
STL中的每个算法都非常精妙,接下来的几天我想集中学习一下STL中的算法. ForwardIter lower_bound(ForwardIter first, ForwardIter last,co ...
- lower_bound和upper_bound算法
参考:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html ForwardIter lower_bound(ForwardIte ...
- [转] STL源码学习----lower_bound和upper_bound算法
http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html PS: lower_bound of value 就是最后一个 < ...
- lower_bound和upper_bound算法实现
lower_bound算法要求在已经按照非递减顺序排序的数组中找到第一个大于等于给定值key的那个数,其基本实现原理是二分查找,如下所示: int lower_bound(vector<int& ...
- STL algorithm算法lower_bound和upper_bound(31)
lower_bound原型: function template <algorithm> std::lower_bound default (1) template <class F ...
- LeetCode 34 - 在排序数组中查找元素的第一个和最后一个位置 - [二分][lower_bound和upper_bound]
给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n) 级别. 如果数组中不存在目标值,返回 [ ...
- lower_bound() 与 upper_bound()
1. lower_bound() lower_bound()是泛型算法,在使用时,需要先将序列进行排序: 作用: 函数lower_bound()在first和last中的前闭后开区间进行二分查找,返 ...
- [STL] lower_bound和upper_bound
STL中的每个算法都非常精妙, ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一 ...
随机推荐
- nginx proxy_pass 里的”/”
见配置,摘自nginx.conf 里的server 段: server { listen 80; server_name abc.163.com ; location / { proxy_pass h ...
- django模板加载静态资源
1. 目录结构 /mysite/setting.py部分配置: # Django settings for mysite project. import os.path TEMPLATE_DIRS = ...
- poj1861 最小生成树 prim & kruskal
// poj1861 最小生成树 prim & kruskal // // 一个水题,为的仅仅是回味一下模板.日后好有个照顾不是 #include <cstdio> #includ ...
- BI测试
BI概念: 商业智能(Business Intelligence 简称BI),指数据仓库相关技术与应用的通称.指利用各种智能技术,来提升企业的商业竞争力.是帮助企业更好地利用数据提高决策质量的技术,包 ...
- Pairs of Integers
Pairs of Integers You are to find all pairs of integers such that their sum is equal to the given in ...
- Leftmost Digit(hdu1060)(数学题)
Leftmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- WSGI协议解析
WSGI协议中包含两个角色:服务器方和应用程序: 服务器方:其调用应用程序,给应用程序提供(环境信息)和(回调函数), 这个回调函数是用来将应用程序设置的http header和status等信息传递 ...
- 记录-在jsp页面获取后台值在页面显示过长处理
在下面的红色标记处 后台获取的值(字符串)在页面显示过长或者与其他重叠 (xxx).cutStr(15) 15代表的是展示字符串的长度 data.rows[i].avgPrice, ), data.r ...
- 九度OJ 1334:占座位 (模拟)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:864 解决:202 题目描述: sun所在学校的教室座位每天都是可以预占的. 一个人可以去占多个座位,而且一定是要连续的座位,如果占不到他所 ...
- Feign-独立使用-实战
目录 写在前面 1.1.1. 短连接API的接口准备 1.1.2. 申明远程接口的本地代理 1.1.3. 远程API的本地调用 写在最后 疯狂创客圈 亿级流量 高并发IM 学习实战 疯狂创客圈 Jav ...