当前位置:网站首页>LeetCode 1346、检查整数及其两倍数是否存在
LeetCode 1346、检查整数及其两倍数是否存在
2022-04-23 20:23:00 【亡于灬】
1346、检查整数及其两倍数是否存在
1)题目描述
给你一个整数数组 arr
,请你检查是否存在两个整数 N
和 M
,满足 N
是 M
的两倍(即,N = 2 * M
)。
更正式地,检查是否存在两个下标 i
和 j
满足:
i != j
0 <= i, j < arr.length
arr[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
边栏推荐
- Es index (document name) fuzzy query method (database name fuzzy query method)
- Use the rolling division method to find the maximum common divisor of two numbers
- Numpy sort search count set
- DNS cloud school | analysis of hidden tunnel attacks in the hidden corner of DNS
- Azkaban recompile, solve: could not connect to SMTP host: SMTP 163.com, port: 465 [January 10, 2022]
- NC basic usage 3
- After route link navigation, the sub page does not display the navigation style problem
- 【PTA】整除光棍
- 论文写作 19: 会议论文与期刊论文的区别
- selenium.common.exceptions.WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT
猜你喜欢
Three. Based on ply format point cloud voxel model JS upload interface writing
Shanghai responded that "flour official website is an illegal website": neglect of operation and maintenance has been "hacked", and the police have filed a case
A useless confession artifact
Livego + ffmpeg + RTMP + flvjs to realize live video
Automatically fill in body temperature and win10 task plan
DTMF双音多频信号仿真演示系统
SQL Server Connectors By Thread Pool | DTSQLServerTP 插件使用说明
Recognition of high-speed road signs by Matlab using alexnet
Es error: request contains unrecognized parameter [ignore_throttled]
DNS cloud school | analysis of hidden tunnel attacks in the hidden corner of DNS
随机推荐
bounding box iou
Record: call mapper to report null pointer Foreach > the usage of not removing repetition;
Tencent Qiu Dongyang: techniques and ways of accelerating deep model reasoning
波场DAO新物种下场,USDD如何破局稳定币市场?
After route link navigation, the sub page does not display the navigation style problem
Analysis of the relationship between generalized Bim and CAD under the current background
The second method of file upload in form form is implemented by fileitem class, servletfileupload class and diskfileitemfactory class.
Scripy tutorial - (2) write a simple crawler
ABAQUS script email auto notification
Wave field Dao new species end up, how does usdd break the situation and stabilize the currency market?
star
JDBC tool class jdbcconutil gets the connection to the database
【PTA】L2-011 玩转二叉树
Redis cache penetration, cache breakdown, cache avalanche
Building the tide, building the foundation and winning the future -- the successful holding of zdns Partner Conference
Computing the intersection of two planes in PCL point cloud processing (51)
R language ggplot2 visualization: ggplot2 visualizes the scatter diagram and uses geom_ mark_ The ellipse function adds ellipses around data points of data clusters or data groups for annotation
R language ggplot2 visual facet_wrap, and use the lineheight parameter to customize the height of the facet icon tab (gray label bar)
Why does ES6 need to introduce map when JS already has object type
Numpy mathematical function & logical function