Arrays.binarySearch 数组二分查找
public static void main(String[] args) throws Exception {
/**
* binarySearch(Object[], Object key)
a: 要搜索的数组
key:要搜索的值
如果key在数组中,则返回搜索值的索引;否则返回-1或“-”(插入点)。插入点是索引键将要插入数组的那一点,即第一个大于该键的元素的索引。
技巧:
[1] 搜索值不是数组元素,且在数组范围内,从1开始计数,得“ - 插入点索引值”;
[2] 搜索值是数组元素,从0开始计数,得搜索值的索引值;
[3] 搜索值不是数组元素,且大于数组内元素,索引值为 – (length + 1);
[4] 搜索值不是数组元素,且小于数组内元素,索引值为 – 1。
binarySearch(Object[], int fromIndex, int toIndex, Object key)如上
*/
int arr [] = new int[]{1,3,4,5,8,9};
System.out.println(arr.length+1);
Arrays.sort(arr);
int index1 = Arrays.binarySearch(arr,6);
int index2 = Arrays.binarySearch(arr,4);
int index3 = Arrays.binarySearch(arr,0);
int index4 = Arrays.binarySearch(arr,10);
System.out.println("index1="+index1);
System.out.println("index2="+index2);
System.out.println("index3="+index3);
System.out.println("index4="+index4);
}
结果如下
index1=-5
index2=2
index3=-1
index4=-7
Arrays.binarySearch 数组二分查找的更多相关文章
- Java中数组Arrays.binarySearch,快速查找数组内元素位置
在数组中查找一个元素,Arrays提供了一个方便查询的方法.Arrays.binarySearch(): 测试列子: public class MainTestArray { public stati ...
- [LeetCode] #167# Two Sum II : 数组/二分查找/双指针
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
- 递归分治算法之二维数组二分查找(Java版本)
[java] /** * 递归分治算法学习之二维二分查找 * @author Sking 问题描述: 存在一个二维数组T[m][n],每一行元素从左到右递增, 每一列元素从上到下递增,现在需要查找元素 ...
- POJ 2182 Lost Cows (树状数组 && 二分查找)
题意:给出数n, 代表有多少头牛, 这些牛的编号为1~n, 再给出含有n-1个数的序列, 每个序列的数 ai 代表前面还有多少头比 ai 编号要小的牛, 叫你根据上述信息还原出原始的牛的编号序列 分析 ...
- PAT-1057 Stack (树状数组 + 二分查找)
1057. Stack Stack is one of the most fundamental data structures, which is based on the principle of ...
- programmercarl——数组——二分查找
二分查找,在经过: 34--https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-arr ...
- toj 4353 Estimation(树状数组+二分查找)
Estimation 时间限制(普通/Java):5000MS/15000MS 运行内存限制:65536KByte总提交: 6 测试通过: 1 描述 “There are ...
- [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针
一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...
- java二分查找
二分查找又称折半查找,优点是比较次数少,查找速度快,平均性能好:其缺点是要求待查表为有序表,且插入删除困难.因此,折半查找方法适用于不经常变动而查找频繁的有序列表.首先,假设表中元素是按升序排列,将表 ...
随机推荐
- MVC的好处 演示
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- jsp页面编写锚点,和html页面编写锚点
html锚点的编写方式,在jsp中不兼容.因此在写动态网页时,需要注意 一:html页面中的锚点编写方式 HTML锚点 <a href="#abc">goto1< ...
- 枚举大小为k的子集
这种位操作不大可能分析出来,先看代码再分析. 代码 使用条件:\(k>0\) void solve(int n,int k) { for(int comb = (1 << k) - ...
- Nolia 给CC添加过滤器
思路: 1.使用jqurey-tagput ,做得不好看,领导不满意 2.使用bootstrap select2这个控件, 思路: 1.添加css和js的文件 2.添加标签的时候,根据id拼接标签,a ...
- Eclipse+Spring学习(一)环境搭建(转)
最近由于投了一家公司实习,他要java工程师,而我大学3年的精力都花到了ASP.NET和前端上面,到找工作的时候才发现大公司不要.NET的,所以马上转型java...由于网上的高手都不屑于写这类文章, ...
- USB gadget 驱动 printer.c 分析
1. modprobe g_printer idVendor=0x0525 idProduct=0xa4a8 modprobe后面也可以加模块参数 2. prn_example从stdout获取数据然 ...
- Oracle中的三种Join 方式
基本概念 Nested loop join: Outer table中的每一行与inner table中的相应记录join,类似一个嵌套的循环. Sort merge join: 将两个表排序,然后再 ...
- 上产使用MQ的三点注意
安全性考量:在正式环境中使用消息队列中间件服务一定要做相关的安全性设置.包括启用消息队列服务的用户名和密码.启用消息队列服务自带的SSL加密设置.如果您使用的消息队列服务不自带SSL加密,则一定要自己 ...
- async(await)知识点
async 函数是 Generator 函数的语法糖. async 函数对 Generator 函数的改进体现在: async 内置执行器. Generator 函数的执行必须靠执行器,需要调用 ne ...
- MySQL 查看编码 排序规则
查看数据库的排序规则 mysql> show variables like 'collation%'; +----------------------+-------------------+ ...