Java实现 lower_bound() 和 upper_bound()
Java实现 lower_bound() 和 upper_bound()
lower_bound()
函数 lower_bound() 在 [begin, end) 进行二分查找,返回 大于或等于 tar的第一个元素位置。如果所有元素都小于tar,则返回 end.
public class LowerBound {
public static int lower_bound(int[] arr, int begin, int end, int tar) {
while(begin < end) {
int mid = begin + (end - begin) / 2;
// 当 mid 的元素小于 tar 时
if(arr[mid] < tar)
// begin 为 mid + 1, arr[begin] 的值会小于或等于 tar
begin = mid + 1;
// 当 mid 的元素大于等于 tar 时
else if(arr[mid] >= tar)
end = mid;
}
return begin;
}
}
upper_bound()
函数 upper_bound() 在 [begin, end) 进行二分查找,返回 大于 tar的第一个元素位置。如果所有元素都小于tar,则返回 end.
public class UpperBound {
public static int upper_bound(int[] arr, int begin, int end, int tar) {
while(begin < end) {
int mid = begin + (end - begin) / 2;
if(arr[mid] <= tar)
begin = mid + 1;
else if(arr[mid] < tar)
end = mid;
}
return begin;
}
}
参考链接
[sumy的博客](http://sumygg.com/2017/09/08/upper-bound-and-lower-bound-in-java/)
Java实现 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中的每个算法都非常精妙, ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一 ...
- vector的插入、lower_bound、upper_bound、equal_range实例
对于这几个函数的一些实例以便于理解: #include <cstdlib> #include <cstdio> #include <cstring> #includ ...
- STL中的lower_bound和upper_bound的理解
STL迭代器表述范围的时候,习惯用[a, b),所以lower_bound表示的是第一个不小于给定元素的位置 upper_bound表示的是第一个大于给定元素的位置. 譬如,值val在容器内的时候,从 ...
- STL 源码分析《5》---- lower_bound and upper_bound 详解
在 STL 库中,关于二分搜索实现了4个函数. bool binary_search (ForwardIterator beg, ForwardIterator end, const T& v ...
- lower_bound和upper_bound算法
参考:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html ForwardIter lower_bound(ForwardIte ...
- lower_bound 和 upper_bound
Return iterator to lower bound Returns an iterator pointing to the first element in the range [first ...
- STL源码学习----lower_bound和upper_bound算法[转]
STL中的每个算法都非常精妙,接下来的几天我想集中学习一下STL中的算法. ForwardIter lower_bound(ForwardIter first, ForwardIter last,co ...
- [转] STL源码学习----lower_bound和upper_bound算法
http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html PS: lower_bound of value 就是最后一个 < ...
随机推荐
- 手贱--npm 误改全局安装路径
修改全局安装命令: 通过 npm config set prefix "目录路径" 来设置. 通过 npm config get prefix 来获取当前设置的目录. 我的node ...
- Regular Expression学习笔记
正则写法 var re = /a/;//简写 /.../里不能为空,因为会误以为是注释: var re = new RegExp('a'); 新建一个RegExp对象:和新建Array对象,Objec ...
- 软件项目技术点(7)——在canvas上绘制自定义图形
AxeSlide软件项目梳理 canvas绘图系列知识点整理 图形种类 目前我们软件可以绘制出来的形状有如下这几种,作为开发者我们一直想支持用户可以拖拽的类似word里面图形库,但目前还没有找到比 ...
- hdu 1010 Tempter of the Bone(dfs)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- 【Android】Warning :uninstalling will remove the application data!
最近从Android Studio向手机发布项目过程中经常出现, 问题虽小,但是开发过程中确实浪费时间. It is possible that issue is resolved by uninst ...
- [uva] 10099 - The Tourist Guide
10099 - The Tourist Guide 题目页:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemi ...
- 天诛进阶之D算法 #3700
http://mp.weixin.qq.com/s/ngn98BxAOLxXPlLU8sWH_g 天诛进阶之D算法 #3700 2015-11-24 yevon_ou 水库论坛 天诛进阶之D算法 #3 ...
- html 页面清浏览器缓存
<meta http-equiv="Pragma" content="no-cache" /> <meta http-equiv=" ...
- java、C语言实现数组模拟栈
java: public class ArrayStack { private int[] data; private int top; private int size; public ArrayS ...
- leetcode-wildcard matching-ZZ
http://yucoding.blogspot.com/2013/02/leetcode-question-123-wildcard-matching.html 几个例子: (1) acbdeabd ...