当前位置:网站首页>LeetCode->二分查找打卡(三)
LeetCode->二分查找打卡(三)
2022-07-26 23:17:00 【月半的人】
public boolean isPerfectSquare(int num) {
int left = 1;
int right = num;
while(left<=right){
int middle = left+(right-left)/2;
long tem = (long)middle*middle;
if(tem==num){
return true;
}else if(tem>num){
right = middle-1;
}else{
left = middle+1;
}
}
return false;
}这道题的关键就在于->利用二分法在arr2中找到距离arr1[X]最相近的值,但是是要找的值是第一个大于等于arr1[x],并且,该值得前面的一个数是第一个小于等于arr1[x] 的值。
同时如果我们对于arr2进行排序之后,如果arr2的最后一个值表示的是小于arr1[x]那么就不需要比较了,
class Solution {
//1 2 3 4
//1 8 9 10
public int findTheDistanceValue(int[] arr1, int[] arr2, int d) {
Arrays.sort(arr2);
int count = 0;
for(int x : arr1){
int index = binarySearch(arr2,x);
boolean flag = true;
if(index<arr2.length){
flag&=arr2[index]-x>d;
}
if(index-1>=0 && index-1<arr2.length){
flag &= x-arr2[index-1]>d;
}
count+=flag?1:0;
}
return count;
}
private int binarySearch(int[] array,int target){
int left = 0;
int right = array.length-1;
while(left<right){
int middle = left+(right-left)/2;
if(array[middle]>target){
right = middle;
}else{
left = middle+1;
}
}
return left;
}
}边栏推荐
- Solve every bit of an integer
- HCIP-第六天-OSPF静态大实验
- [enchanting interpretation, 15 minutes let you thoroughly learn how to use the stack!!!]
- JVM interview questions (necessary for interview)
- NAT network address conversion experiment
- C language - characters and strings, arithmetic operators, type conversions
- Guangguangzai's CSDN journey
- HCIP-第五天-OSPF扩展配置实验
- 通过ensp让静态路由实现全网可达
- [do you know cache - fully understand cache]
猜你喜欢

Hcip the next day

Record the nth SQL exception

JMeter下载安装

砺夏行动|源启数字化:既有模式,还是开源创新?

【你了解Cache吗——全面理解高速缓冲存储器】
![[brother Yang takes you to play with the linear table (III) - two way linked list]](/img/64/5367ff4fb6797565cb1f9e1a8aee4e.png)
[brother Yang takes you to play with the linear table (III) - two way linked list]

Risc-v tool chain compilation notes

C language - characters and strings, arithmetic operators, type conversions

Hcip day 3 Wan topology experiment
![[C language programming] branch structure](/img/17/f9d5360d8802d29079dcbfcc302003.png)
[C language programming] branch structure
随机推荐
Area optimization of digital chips: detailed explanation of question 1 in the digital direction of the third "Huawei Cup" graduate innovation core competition
Graduated and entered HW, from test engineer to project manager. Now I earn millions in goose factory every year. My suggestions to you
N methods of SQL optimization
OSPF summary (mind map)
I wish you a happy Chinese Valentine's day and invite you to read the source code together
Summary of dataset operations in ppocrlabel format.
uni-app上自定义微信小程序的tabbar
Witness that the "decoding 2022 strong star of China's network security" is about to set sail
面试必问 | 一个线程从创建到消亡要经历哪些阶段?
全连MGRE与星型拓扑MGRE
Hcip day 1
After ten years of testing, I want to say to my friends who are still confused: one thing is to do a good job in personal planning
见证中国网安力量 “解码2022中国网安强星”即将启航
【斐波那契数列及螺线 基于C语言】
NAT network address conversion experiment
The problem of storing elements in TreeSet collection
[Fibonacci sequence and spiral are based on C language]
毕业进入HW,从测试工程师到项目经理,现如今在鹅厂年收入百万,我的给大家的一些建议...
C语言 学生信息管理系统 基于数组 可以存取到文本文件
Redis distributed lock implemented by annotation