当前位置:网站首页>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
边栏推荐
- Es error: request contains unrecognized parameter [ignore_throttled]
- WordPress plug-in: WP CHINA Yes solution to slow domestic access to the official website
- ArcGIS JS version military landmark drawing (dovetail arrow, pincer arrow, assembly area) fan and other custom graphics
- ABAQUS script email auto notification
- Analysis of the relationship between generalized Bim and CAD under the current background
- PCL点云处理之直线与平面的交点计算(五十三)
- [talkative cloud native] load balancing - the passenger flow of small restaurants has increased
- Azkaban recompile, solve: could not connect to SMTP host: SMTP 163.com, port: 465 [January 10, 2022]
- Is the wechat CICC wealth high-end zone safe? How to open an account for securities
- Numpy mathematical function & logical function
猜你喜欢
SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
DNS cloud school rising posture! Three advanced uses of authoritative DNS
【PTA】整除光棍
C migration project record: modify namespace and folder name
After route link navigation, the sub page does not display the navigation style problem
2022dasctf APR x fat epidemic prevention challenge crypto easy_ real
SIGIR'22 "Microsoft" CTR estimation: using context information to promote feature representation learning
Scripy tutorial - (2) write a simple crawler
考研英语唐叔的语法课笔记
Es keyword sorting error reason = fielddata is disabled on text fields by default Set fielddata = true on keyword in order
随机推荐
SQL gets the latest record of the data table
[PTA] l2-011 play with binary tree
SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
Solve the Chinese garbled code of URL in JS - decoding
Is the wechat CICC wealth high-end zone safe? How to open an account for securities
上海回應“面粉官網是非法網站”:疏於運維被“黑”,警方已立案
XXXI` Prototype ` displays prototype properties and`__ proto__` Implicit prototype properties
The market share of the financial industry exceeds 50%, and zdns has built a solid foundation for the financial technology network
selenium. common. exceptions. WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT
Change the material of unity model as a whole
Numpy mathematical function & logical function
DNS cloud school rising posture! Three advanced uses of authoritative DNS
Scripy tutorial - (2) write a simple crawler
An error is reported when sqoop imports data from Mysql to HDFS: sqlexception in nextkeyvalue
[stack and queue topics] - sliding window
Go zero framework database avoidance Guide
内网渗透之DOS命令
The second method of file upload in form form is implemented by fileitem class, servletfileupload class and diskfileitemfactory class.
Zdns was invited to attend the annual conference of Tencent cloud basic resources and share the 2020 domain name industry development report
Plato Farm元宇宙IEO上线四大,链上交易颇高