当前位置:网站首页>LeetCode 1346、检查整数及其两倍数是否存在
LeetCode 1346、检查整数及其两倍数是否存在
2022-04-23 20:23:00 【亡于灬】
1346、检查整数及其两倍数是否存在
1)题目描述
给你一个整数数组 arr,请你检查是否存在两个整数 N 和 M,满足 N 是 M 的两倍(即,N = 2 * M)。
更正式地,检查是否存在两个下标 i 和 j 满足:
i != j0 <= i, j < arr.lengtharr[i] == 2 * arr[j]
示例 1:
输入:arr = [10,2,5,3]
输出:true
解释:N = 10 是 M = 5 的两倍,即 10 = 2 * 5 。
示例 2:
输入:arr = [7,1,14,11]
输出:true
解释:N = 14 是 M = 7 的两倍,即 14 = 2 * 7 。
示例 3:
输入:arr = [3,1,7,11]
输出:false
解释:在该情况下不存在 N 和 M 满足 N = 2 * M 。
提示:
2 <= arr.length <= 500-10^3 <= arr[i] <= 10^3
2)分析
排序,二分查找。
- 先对数组进行排序;
- 再通过二分查找数组中是否存在等于当前元素二倍的元素,在二分查找中,为了避免
0导致出错,要排除当前元素本身。
3)C++代码
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;
}
};
版权声明
本文为[亡于灬]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_38342510/article/details/124360356
边栏推荐
- Mathematical modeling column | Part 5: MATLAB optimization model solving method (Part I): Standard Model
- Sqoop imports tinyint type fields to boolean type
- Mysql database and table building: the difference between utf8 and utf8mb4
- [target tracking] pedestrian attitude recognition based on frame difference method combined with Kalman filter, with matlab code
- ArcGIS JS version military landmark drawing (dovetail arrow, pincer arrow, assembly area) fan and other custom graphics
- Change the material of unity model as a whole
- Cadence Orcad Capture CIS更换元器件之Link Database 功能介绍图文教程及视频演示
- How about CICC fortune? Is it safe to open an account
- Devexpress 14.1 installation record
- An error is reported when sqoop imports data from Mysql to HDFS: sqlexception in nextkeyvalue
猜你喜欢
![Es error: request contains unrecognized parameter [ignore_throttled]](/img/17/9131c3eb023b94b3e06b0e1a56a461.png)
Es error: request contains unrecognized parameter [ignore_throttled]

Leetcode dynamic planning training camp (1-5 days)

Latest investigation and progress of building intelligence based on sati

Customize timeline component styles

SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions

Es keyword sorting error reason = fielddata is disabled on text fields by default Set fielddata = true on keyword in order

The ODB model calculates the data and outputs it to excel

Linux64Bit下安装MySQL5.6-不能修改root密码

Commit and ROLLBACK in DCL of 16mysql

Modeling based on catiav6
随机推荐
Numpy mathematical function & logical function
Mysql database backup scheme
star
Thirty What are VM and VC?
微信中金财富高端专区安全吗,证券如何开户呢
selenium. common. exceptions. WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT
Introduction to link database function of cadence OrCAD capture CIS replacement components, graphic tutorial and video demonstration
Implementation of mypromise
R language uses timeroc package to calculate the multi time AUC value of survival data under competitive risk, uses Cox model and adds covariates, and R language uses the plotauccurve function of time
How about CICC fortune? Is it safe to open an account
WordPress plug-in: WP CHINA Yes solution to slow domestic access to the official website
ArcGIS js api 4. X submergence analysis and water submergence analysis
bounding box iou
[problem solving] 'ASCII' codec can't encode characters in position XX XX: ordinal not in range (128)
6-5 字符串 - 2. 字符串复制(赋值) (10 分)C语言标准函数库中包括 strcpy 函数,用于字符串复制(赋值)。作为练习,我们自己编写一个功能与之相同的函数。
Tencent Qiu Dongyang: techniques and ways of accelerating deep model reasoning
Latest investigation and progress of building intelligence based on sati
After route link navigation, the sub page does not display the navigation style problem
What is the difference between a host and a server?
AQS learning