二分查找、upper_bound、lower_bound
整理及总结二分查找的判断和边界细节
修改版
package com.leej.binarysearch;
import java.util.Arrays;
/**
* @author jerry
* @create 17/10/7 12:21
*/
public class BinarySearch {
public static int BinarySearch(int[] nums, int key) {
int start = 0, end = nums.length - 1;
int mid;
while(start <= end) {
mid = (start + end) >> 1;
if (nums[mid] == key) return mid;
else if (nums[mid] > key)
end = mid -1;
else
start = mid + 1;
}
return -(start + 1);
}
public static int LowerBound(int[] nums, int key) {
int first = 0, last = nums.length;
int mid;
while(first < last) {
mid = (first + last) >> 1;
if (nums[mid] < key) {
first = mid + 1;
} else {
last = mid;
}
}
return first;
}
public static int UpperBound(int[] nums, int key) {
int first = 0, last = nums.length;
int mid;
while(first < last) {
mid = (first + last) >> 1;
if (nums[mid] <= key) {
first = mid + 1;
} else {
last = mid;
}
}
return first;
}
public static void showArrays(int[] nums) {
for(int num : nums) System.out.print(num + " ");
System.out.println();
}
public static void main(String[] args) {
int[] nums = {10,20,30,30,20,10,10,20, 10};
Arrays.sort(nums); //10 10 10 20 20 20 30 30
showArrays(nums);
//System.out.println( BinarySearch(nums, 11) );
System.out.println(LowerBound(nums, 21));
System.out.println(UpperBound(nums, 21));
}
}
实现
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Arrays;
import java.lang.String;
/**
* @author jerry
* @create
*/
public class test {
public static void showArray(int[] nums) {
if (nums == null || nums.length == 0) return;
for (int nu : nums)
System.out.printf("%d ", nu);
System.out.println();
}
/**
* 循环条件left<=right, 所以left != mid , right != mid;
* 双闭区间[left, right]
**/
public static int binarySearch(int[] nums, int target) {
int left = 0, right = nums.length - 1, mid; //search range [left, right],闭区间
while(left <= right) {
mid = (left + right) >> 1;
if (nums[mid] == target)
return mid;
else if (nums[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}
/**
**STL 版本lower_bound
* 找到第一个大于等于target的数, in range [first, last), 左开右闭区间
**/
public static int lower_bound(int[] nums, int target) {
int first = 0, last = nums.length;
int mid, len, step;
len = last - first;
while(len > 0) {
step = len >> 1;
mid = first + step;
if (nums[mid] < target) { //[mid + 1, last)
first = mid + 1;
len -= step + 1;
} else { //[first, mid)最后可取边界mid
len = step;
}
}
return first;
}
//找到第一个大于等于target
public static int my_lower_bound(int[] nums, int target) {
int first = 0, last = nums.length, mid;
//in ranget [first, last)
while(first < last) {
mid = (first + last) >> 1;
if (nums[mid] < target) { //[mid+1, last)
first = mid + 1;
} else {
last = mid;
}
}
return first;
}
//找到第一个大于target的数
public static int my_upper_bound(int[] nums, int target) {
int first = 0, last = nums.length, mid;
//in range [first, last)
while(first < last) {
mid = (first + last) >> 1;
if (nums[mid] <= target) {
first = mid + 1;
} else {
last = mid;
}
}
return first;
}
public static void main( String args[] ){
int[] nums = {10,20,30,30,20,10,10,20, 10};
Arrays.sort(nums); //10 10 10 20 20 20 30 30
test.showArray(nums);
System.out.println( test.binarySearch(nums, 11) );
System.out.println( test.my_lower_bound(nums, 20) );
System.out.println( test.lower_bound(nums, 20) );
System.out.println( test.my_upper_bound(nums, 20) );
}
}
//#output
// 10 10 10 10 20 20 20 30 30
// -1
// 4
// 4
// 7
References
- http://www.cplusplus.com/reference/algorithm/lower_bound/
- http://www.cplusplus.com/reference/algorithm/upper_bound/
- http://www.cplusplus.com/reference/algorithm/binary_search/
二分查找、upper_bound、lower_bound的更多相关文章
- C++二分查找:lower_bound( )和upper_bound( )
#include<algorithm>//头文件 //标准形式 lower_bound(int* first,int* last,val); upper_bound(int* first, ...
- 二分查找(lower_bound和upper_bound)
转载自:https://www.cnblogs.com/luoxn28/p/5767571.html 1 二分查找 二分查找是一个基础的算法,也是面试中常考的一个知识点.二分查找就是将查找的键和子数组 ...
- 二分查找确定lower_bound和upper_bound
lower_bound当target存在时, 返回它出现的第一个位置,如果不存在,则返回这样一个下标i:在此处插入target后,序列仍然有序. 代码如下: int lower_bound(int* ...
- 关于二分查找 使用 lower_bound
在寻找单调递增最长自序列 , 的时候能不能确认出来哪个是单调递增最长自序列 ? 我的想法是 if(location>=num) dp[location]=b; 这样的 , 基于http:// ...
- LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- CodeForces - 600B Queries about less or equal elements (二分查找 利用stl)
传送门: http://codeforces.com/problemset/problem/600/B Queries about less or equal elements time limit ...
- I Count Two Three(打表+排序+二分查找)
I Count Two Three 二分查找用lower_bound 这道题用cin,cout会超时... AC代码: /* */ # include <iostream> # inclu ...
- STL 二分查找三兄弟(lower_bound(),upper_bound(),binary_search())
一:起因 (1)STL中关于二分查找的函数有三个:lower_bound .upper_bound .binary_search -- 这三个函数都运用于有序区间(当然这也是运用二分查找的前提),以 ...
- STL中的二分查找———lower_bound,upper_bound,binary_search
关于STL中的排序和检索,排序一般用sort函数即可,今天来整理一下检索中常用的函数——lower_bound , upper_bound 和 binary_search . STL中关于二分查找的函 ...
- 二分查找法(binary_search,lower_bound,upper_bound,equal_range)
binary_search(二分查找) //版本一:调用operator<进行比较 template <class ForwardIterator,class StrictWeaklyCo ...
随机推荐
- linux查看系统版本(适用于centos、ubutun,其他类型没有进行测试)
方法一:cat /etc/issue 或more /etc/issue root@salt-master:~# cat /etc/issueUbuntu 16.04.2 LTS \n \l 方法二:l ...
- Codeforces 126B(kmp)
要点 头尾的最长相同只要一个kmp即可得,于是处理中间部分 扫一遍记录一下前缀的每个位置是否存在一个中间串跟它相同,见代码 如果当前没有,接着用Next数组去一找即可 #include <cst ...
- OAuthLogin2.0
开源第三方登录组件OAuthLogin2.0 支持QQ,阿里巴巴,淘宝,京东,蘑菇街,有赞等平台 Nuget地址:https://www.nuget.org/packages/OAuthLogin ...
- 课程增加功能(java web)
1.设计思想 先写类DBUtil用来连接数据库.在UserDaoImpl2类中写在数据库中添加课程表信息的方法.然后定义类Calss2来写保存超级课表数据:课程名称,任课教师,上课地点的属性及其get ...
- 059 Spiral Matrix II 旋转打印矩阵 II
给出正整数 n,生成正方形矩阵,矩阵元素为 1 到 n2 ,元素按顺时针顺序螺旋排列.例如,给定正整数 n = 3,应返回如下矩阵:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6 ...
- Apache Atlas是什么?
不多说,直接上干货! Apache Atlas是Hadoop社区为解决Hadoop生态系统的元数据治理问题而产生的开源项目,它为Hadoop集群提供了包括数据分类.集中策略引擎.数据血缘.安全和生命周 ...
- .net笔试题二(填空题、选择题)
1.面向对象的语言具有_______性.________性._______性答:封装.继承.多态. 2.能用foreach遍历访问的对象需要实现 ____________接口或声明__________ ...
- [拾零]C语言的数组指针
为了强化记忆,从而写笔记保留. 数组指针,顾名思义,是在说一个指针,这个指针是指向数组的. 区别于指针数组 int* p[5] = NULL; //指针数组 基类型 int* int (*p)[5] ...
- Android安卓电话拦截及短信过滤
package com.focus.manager; import java.lang.reflect.Method; import Android .app.Activity; import And ...
- Linux 安装Memcache扩展支持
查看相关软件包 yum search memcached 安装memcache yum -y install memcachedMemcache关联php yum -y install php-pec ...