当前位置:网站首页>每日3题(3)-检查整数及其两倍数是否存在
每日3题(3)-检查整数及其两倍数是否存在
2022-06-25 10:24:00 【程序猿不脱发2】
题目:
给你一个整数数组 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
思路:
先将所有数字存入哈希表,再遍历所有的数字 xx,判断 2x2x 是否在哈希表中。
java代码:
class Solution {
public boolean checkIfExist(int[] arr) {
HashSet<Integer> set = new HashSet<>();
for (int i : arr) {
if (set.contains(2 * i) || (i % 2 == 0 && set.contains(i / 2)))
return true;
set.add(i);
}
return false;
}
}
边栏推荐
- Houdini图文笔记:Your driver settings have been set to force 4x Antialiasing in OpenGL applications问题的解决
- 单片机进阶---PCB开发之照葫芦画瓢(二)
- Think about it
- [paper reading | deep reading] line: large scale information network embedding
- Dependent properties, dependent additional properties, and type conversions
- XSS攻击
- Summary of considerations for native applet development
- 单片机开发---基于ESP32-CAM的人脸识别应用
- Is it safe to open an account through mobile phone if you open an account through stock speculation? Who knows?
- 《天天数学》连载52:二月二十日
猜你喜欢
随机推荐
Binder explanation of Android interview notes
单片机开发---基于ESP32-CAM的人脸识别应用
Kotlin arrays and collections (1) {create arrays, use arrays, use for in loops to traverse arrays, use array indexes, and multi-dimensional arrays}
New school: no fraud Economics
国信证券证券账户开户安全吗
The title of my composition is - "my district head father"
Is it safe to open a stock account on the compass?
ShardingSphere-Proxy 4.1 分庫分錶
OpenCV学习(一)---环境搭建
Learn to learn self-study [learning to learn itself is more important than learning anything]
手机炒股安全吗?
Request&Response有这一篇就够了
[RPC] i/o model - Rector mode of bio, NiO, AIO and NiO
i++ 和 ++i的真正区别
Your driver settings have been set to force 4x antialiasing in OpenGL applications
The path of Architects
tokenizers>=0.11.1,!=0.11.3,<0.13 is required for a normal functioning of this module,
Flask博客实战 - 实现侧边栏最新文章及搜索
Performance memory
Basic usage and principle of schedulemaster distributed task scheduling center