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. Hadoop自带Sort例子分析

    /** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agree ...

  2. java.io.IOException: Illegal partition for 67 (-1)

    今天写MapReduce的分区进行排序的功能,自己写了一个Partitioner,然后用的时候就错了 public static class MyPartition extends Partition ...

  3. centos6搭建docker镜像私服

    1.创建私服容器 docker run -d -e SETTINGS_FLAVOR=dev -e STORAGE_PATH=/tmp/registry -v /opt/data/registry:/t ...

  4. eclipse配置python插件

    eclipse配置python主要可以分为以下几个步骤完成: 1. 安装python,python主要有两个版本,python2和python3,这里安装的是python2.7.主要考虑python使 ...

  5. 旋转卡壳求两个凸包最近距离poj3608

    #include <iostream> #include <cmath> #include <vector> #include <string.h> # ...

  6. <转载> 为什么在Python里推荐使用多进程而不是多线程?

    经常我们会听到老手说:“Python下多线程是鸡肋,推荐使用多进程!”,但是为什么这么说呢?                要知其然,更要知其所以然.所以有了下面的深入研究: 首先强调背景:     ...

  7. Spring中的国际化资源以及视图跳转

    一.SpringMVC对国际化的支持 SpringMVC进行资源国际化主要是通过ResourceBundleMessageSource实现的,xml如下配置: <bean id="me ...

  8. 信息搜集之google语法

    总结的比较全,无耻的转了.D: http://blog.csdn.net/chaosa/article/details/1828301 说起Google,可谓无人不知无人不晓.作为世界第一的搜索引擎, ...

  9. 基于flask的web微信

    web微信 1.扫码获取头像 当你打开web微信的时候,因为http是无状态的,web微信如何实时的获取用户的扫码动作? 那么这里用到的是长轮询的方式. from flask import Flask ...

  10. ABAP table control例子

    [转自]http://blog.csdn.net/lhx20/article/details/3039909Table control用于在screen上以表格的形式显示数据,在table contr ...