当前位置:网站首页>Leetcode- > 2-point search and clock in (3)
Leetcode- > 2-point search and clock in (3)
2022-07-27 03:54:00 【Half moon person】
Effective complete square number
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;
}The distance between two arrays
The key to this problem is -> Using dichotomy in arr2 Find the distance arr1[X] The closest value , But the value to be found is the first greater than or equal to arr1[x], also , The first number before the value is less than or equal to arr1[x] Value .
At the same time, if we are arr2 After sorting , If arr2 The last value of is less than arr1[x] Then there is no need to compare ,
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;
}
}边栏推荐
- Explain tool actual operation
- C language force deduction question 43 string multiplication. Optimized vertical
- Add support for @data add-on in idea
- Characteristics and experimental suggestions of abbkine abfluor 488 cell apoptosis detection kit
- Redis source code learning (33), command execution process
- Machine learning [Matplotlib]
- DNS record type and explanation of related terms
- Is Jiufang intelligent investment a regular company? Talk about Jiufang intelligent investment
- Feitengtengrui d2000 won the "top ten hard core technologies" award of Digital China
- On the first day of Shenzhen furniture exhibition, the three highlights of Jin Ke'er booth were unlocked!
猜你喜欢

九方智投是正规公司吗?一起聊聊九方智投

Installation and use of anti-virus software ClamAV

Interview question: the difference between three instantiated objects in string class

Six determination methods of Worthington peroxidase activity

深圳家具展首日,金可儿展位三大看点全解锁!

Contour detection based on OpenCV (2)

Add support for @data add-on in idea

Spark: calculate the average value of the same key in different partitions (entry level - simple implementation)

若依的环境的部署以及系统的运行

About the solution of using hyperbeach to appear /bin/sh: 1: packr2: not found
随机推荐
Redis源码学习(33),命令执行过程
Deployment of ruoyi's environment and operation of the system
How to conduct 360 assessment
Number of square arrays (day 81)
Is Jiufang intelligent investment a regular company? Talk about Jiufang intelligent investment
Installation and use of anti-virus software ClamAV
Principle understanding and application of hash table and consistent hash
GetObject call timing of factorybean
次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
Banyan loan,
Binary tree (Beijing University of Posts and Telecommunications machine test questions) (day85)
Redis spike case, learn from Shang Silicon Valley teacher in station B
Csu18m91 is used as the master controller of the intelligent scale scheme
一种分布式深度学习编程新范式:Global Tensor
榕树贷款C语言结构体里的成员数组和指针
关于使用hyperbeach出现/bin/sh: 1: packr2: not found的解决方案
Do you really understand code rollback?
app端接口用例设计方法和测试方法
DNS record type and explanation of related terms
Process analysis of object creation