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二分查找
二分查找又称折半查找,优点是比较次数少,查找速度快,平均性能好:其缺点是要求待查表为有序表,且插入删除困难.因此,折半查找方法适用于不经常变动而查找频繁的有序列表.首先,假设表中元素是按升序排列,将表 ...
随机推荐
- parseConf(配置文件解析器)
/****************************************************************************** * * parseConf(配置文件解析 ...
- 如何释放vector变量
std::vector<cv::Point> probp; std::vector<int> plabel; plabel.clear(); std::vector<in ...
- 牛客小白月赛2 E:是是非非(尼姆博弈)
链接:https://www.nowcoder.com/acm/contest/86/E来源:牛客网 题目描述 坎为水,险阳失道,渊深不测:离为火,依附团结,光明绚丽.坎卦:水洊至,习坎:君子以常德行 ...
- 《DSP using MATLAB》Problem 4.26
Y(z)部分分式展开, 零状态响应和零输入响应的部分分式展开,
- 《DSP using MATLAB》Problem 4.14
代码: %% ---------------------------------------------------------------------------- %% Output Info a ...
- streamsets 集成 rabbitmq 以及benthos stream 处理框架
benthos 是一个stream 处理框架,streamsets 也是,但是两者可以通过不同的工具进行集成起来 一般我们可以使用http 服务,消息中间件(kafka.rabbitmq ...) 使 ...
- A glance at C# vNext
Contents Introduction Background A list of features Primary constructor Read only auto-properties St ...
- 和为 s 的两个数字(和为 s 的连续正数序列)
题目 输入一个递增排序的数组和一个数字 s,在数组中查找两个数,得它们的和正好是 s.如果有多对数字的和等于 s,输出任意一对即可 思路 我们先在数组中选择两个数字,如果它们的和等于输入的 s,我们就 ...
- 编程写一个方法时,注意方法中传参数的数量最好不要超过5个,超过5个怎么办?可以用struct或class,或一个字典类
图 1 一.从图1发现了什么问题呢? 答案:1.参数传的的太多了:2.另外注释也没写好. 说明:一个方法中,传参数的数量最好不要超过5个. 应该采用:struct或class,或一个字典类都行.其中 ...
- Python——collections模块、time模块、random模块、os模块、sys模块
1. collections模块 (1)namedtuple # (1)点的坐标 from collections import namedtuple Point = namedtuple('poin ...