当前位置:网站首页>Leetcode 1346. Check whether integers and their multiples exist
Leetcode 1346. Check whether integers and their multiples exist
2022-04-23 20:26:00 【Die in a trap】
1346、 Check the existence of integers and their multiples
1) Title Description
Give you an array of integers arr
, Please check whether there are two integers N
and M
, Satisfy N
yes M
Twice as many ( namely ,N = 2 * M
).
More formally , Check if there are two subscripts i
and j
Satisfy :
i != j
0 <= i, j < arr.length
arr[i] == 2 * arr[j]
Example 1:
Input :arr = [10,2,5,3]
Output :true
explain :N = 10 yes M = 5 Twice as many , namely 10 = 2 * 5 .
Example 2:
Input :arr = [7,1,14,11]
Output :true
explain :N = 14 yes M = 7 Twice as many , namely 14 = 2 * 7 .
Example 3:
Input :arr = [3,1,7,11]
Output :false
explain : Does not exist in this case N and M Satisfy N = 2 * M .
Tips :
2 <= arr.length <= 500
-10^3 <= arr[i] <= 10^3
2) analysis
Sort , Two points search .
- Sort the array first ;
- Then, by bisection, we can find whether there is an element in the array equal to twice the current element , In binary search , for fear of
0
Make a mistake , To exclude the current element itself .
3)C++
Code
class Solution {
public:
bool checkIfExist(vector<int>& arr) {
sort(arr.begin(),arr.end());
for(int i=0;i<arr.size();i++){
int left=0;
int right=arr.size()-1;
while(left<=right){
int mid=(left+right)/2;
if(arr[mid]==2*arr[i]&&i!=mid)
return true;
else if(arr[mid]<2*arr[i])
left=mid+1;
else
right=mid-1;
}
}
return false;
}
};
版权声明
本文为[Die in a trap]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204232023107609.html
边栏推荐
- CVPR 2022 | querydet: use cascaded sparse query to accelerate small target detection under high resolution
- Numpy Index & slice & iteration
- [PTA] get rid of singles
- Computing the intersection of two planes in PCL point cloud processing (51)
- star
- Cadence OrCAD capture batch change component packaging function introduction graphic tutorial and video demonstration
- PostgreSQL basic functions
- The construction and use of Fortress machine and springboard machine jumpserver are detailed in pictures and texts
- Thirty What are VM and VC?
- BMP JPEG 图片转换为矢量图像 ContourTrace
猜你喜欢
Plato farm is one of the four largest online IEOS in metauniverse, and the transaction on the chain is quite high
Some basic configurations in interlij idea
Linux64Bit下安装MySQL5.6-不能修改root密码
The construction and use of Fortress machine and springboard machine jumpserver are detailed in pictures and texts
ArcGIS JS version military landmark drawing (dovetail arrow, pincer arrow, assembly area) fan and other custom graphics
An error is reported when sqoop imports data from Mysql to HDFS: sqlexception in nextkeyvalue
Tensorflow 2 basic operation dictionary
Zdns was invited to attend the annual conference of Tencent cloud basic resources and share the 2020 domain name industry development report
Building the tide, building the foundation and winning the future -- the successful holding of zdns Partner Conference
WordPress plug-in: WP CHINA Yes solution to slow domestic access to the official website
随机推荐
I JS deep copy and shallow copy
Modeling based on catiav6
selenium. common. exceptions. WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT
A useless confession artifact
SQL Server Connectors By Thread Pool | DTSQLServerTP 插件使用说明
Parsing methods of JSON data in C - jar and jobobject: error reading jar from jsonreader Current JsonReader item
BMP JPEG picture to vector image contourtrace
go-zero框架数据库方面避坑指南
PIP installation package reports an error. Could not find a version that satisfies the requirement pymysql (from versions: none)
LeetCode 994、腐烂的橘子
LeetCode 20、有效的括号
【PTA】整除光棍
Installation and use of NVM
2022dasctf APR x fat epidemic prevention challenge crypto easy_ real
SIGIR'22 "Microsoft" CTR estimation: using context information to promote feature representation learning
ArcGIS JS version military landmark drawing (dovetail arrow, pincer arrow, assembly area) fan and other custom graphics
JDBC database addition, deletion, query and modification tool class
The flinkcdc reports an error: but this is no longer available on the server
Numpy mathematical function & logical function
Implementation of mypromise