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算法的更多相关文章

  1. STL源码学习----lower_bound和upper_bound算法

    转自:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html 先贴一下自己的二分代码: #include <cstdio&g ...

  2. STL源码学习----lower_bound和upper_bound算法[转]

    STL中的每个算法都非常精妙,接下来的几天我想集中学习一下STL中的算法. ForwardIter lower_bound(ForwardIter first, ForwardIter last,co ...

  3. lower_bound和upper_bound算法

    参考:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html ForwardIter lower_bound(ForwardIte ...

  4. [转] STL源码学习----lower_bound和upper_bound算法

    http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html PS: lower_bound of value 就是最后一个 < ...

  5. lower_bound和upper_bound算法实现

    lower_bound算法要求在已经按照非递减顺序排序的数组中找到第一个大于等于给定值key的那个数,其基本实现原理是二分查找,如下所示: int lower_bound(vector<int& ...

  6. STL algorithm算法lower_bound和upper_bound(31)

    lower_bound原型: function template <algorithm> std::lower_bound default (1) template <class F ...

  7. LeetCode 34 - 在排序数组中查找元素的第一个和最后一个位置 - [二分][lower_bound和upper_bound]

    给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n) 级别. 如果数组中不存在目标值,返回 [ ...

  8. lower_bound() 与 upper_bound()

    1. lower_bound() lower_bound()是泛型算法,在使用时,需要先将序列进行排序: 作用:  函数lower_bound()在first和last中的前闭后开区间进行二分查找,返 ...

  9. [STL] lower_bound和upper_bound

    STL中的每个算法都非常精妙, ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一 ...

随机推荐

  1. Unity Editor Inspector编辑模板

    效果图: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEdito ...

  2. iOS 启动页放大淡出效果

    //屏幕宽度#define kWIDTH [UIScreen mainScreen].bounds.size.width//屏幕高度 #define kHEIGHT [UIScreen mainScr ...

  3. ios8 一些运行问题

     iOS10相册相机闪退bughttp://www.jianshu.com/p/5085430b029fiOS 10 因苹果健康导致闪退 crashhttp://www.jianshu.com/p/5 ...

  4. python mysql orm

    Python中操作mysql的pymysql模块详解:https://www.cnblogs.com/wt11/p/6141225.html Python 12 - Mysql & ORM:h ...

  5. 【bzoj2226】[Spoj 5971] LCMSum 欧拉函数

    题目描述 Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n,n), where LCM(i,n) denotes the Leas ...

  6. 电路分析三------KCL,KVL,VCR方程

    1.2b方程 2.举例 举例2

  7. POJ 2965 The Pilots Brothers' refrigerator【枚举+dfs】

    题目:http://poj.org/problem?id=2965 来源:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26732#pro ...

  8. java面向对象入门之方法参数的传递

    /* Name : Power by :Stuart Date:2015.4.25 */ class PassOn{ //创建show方法,把i传入,输出i+1的结果 public void show ...

  9. statu 设置

    DATA: itab TYPE TABLE OF sy-ucomm. APPEND 'DELE' TO itab. APPEND 'PICK' TO itab. SET PF-STATUS 'STA3 ...

  10. Property Animator基本用法

    ObjectAnimator anim=ObjectAnimator.ofFloat(textview, "alpha", 0f, 1f); //ObjectAnimator an ...