STL: lower_bound(·) and upper_bound(·)
来源:互联网 发布:身份证 复制 知乎 编辑:程序博客网 时间:2024/05/18 20:50
Lower Bound
This is a problem I came across in Google interview, and I was caught off guard. The interviewer asked me to implement the lower_bound(·) function in STL. Here is the code:
int lower_bound(int *array, int size, int key){ int first = 0, middle; int half, len; len = size; while(len > 0) { half = len >> 1; middle = first + half; if(array[middle] < key) { first = middle + 1; len = len-half-1; } else len = half; } return first;}
Upper Bound
Similarly we have:
int upper_bound(int *array, int size, int key){ int first = 0, len = size-1; int half, middle; while(len > 0){ half = len >> 1; middle = first + half; if(array[middle] > key) len = half; else{ first = middle + 1; len = len - half - 1; } } return first;}
0 0
- STL: lower_bound(·) and upper_bound(·)
- C++ STL 之 lower_bound and upper_bound
- C++ STL 之 lower_bound and upper_bound
- C++ STL 之 lower_bound and upper_bound
- C++ STL 之 lower_bound and upper_bound
- stl lower_bound 和 upper_bound
- C++ STL lower_bound upper_bound
- C++ STL lower_bound & upper_bound
- STL lower_bound和upper_bound
- STL lower_bound与upper_bound
- [STL] lower_bound和upper_bound
- lower_bound/upper_bound(STL)
- STL函数 lower_bound / upper_bound
- C++ STL lower_bound & upper_bound
- lower_bound and upper_bound
- lower_bound and upper_bound 返回
- lower_bound() and upper_bound()
- STL 源码分析《5》---- lower_bound and upper_bound 详解
- python --- 列表
- Oracle数据库中日期格式的转换
- Hive DDL(Data Definition Language)之创建表
- Leetcode在线编程reverse-integer
- C++ 友元函数和非成员运算符重载
- STL: lower_bound(·) and upper_bound(·)
- MVP模式
- githubpage+hexo 搭建个人博客
- 地图开发随记
- 面相对象笔记
- 说说算法题的那些小题儿(C语言版)
- spring boot 和 nginx的上传资源大小配置(nginx 413 request entity too large)
- share_ptr智能指针模板类的简单实现(c++11)
- Python_构建发布