当前位置:网站首页>每日3題(3)-檢查整數及其兩倍數是否存在
每日3題(3)-檢查整數及其兩倍數是否存在
2022-06-25 10:48: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 graphic notes: could not create OpenCL device of type (houdini_ocl_devicetype) problem solving
- Oracle彻底卸载的完整步骤
- 一文了解Prometheus
- Floating window --- create a system floating window (can be dragged)
- 单片机进阶---PCB开发之照葫芦画瓢(二)
- Ouverture de l'inscription | le troisième marathon des hackers de pagaie est arrivé comme prévu.
- The left sliding menu +menu item icon is grayed out
- 软件测试 避免“试用期被辞退“指南,看这一篇就够了
- keep-alive
- New school: no fraud Economics
猜你喜欢
随机推荐
Oracle彻底卸载的完整步骤
性能之文件系统篇
The path of Architects
Solutions using protobuf in TS projects
Oracle query comes with JDK version
Kotlin arrays and collections (1) {create arrays, use arrays, use for in loops to traverse arrays, use array indexes, and multi-dimensional arrays}
原生小程序开发注意事项总结
Is it safe to open an account through mobile phone if you open an account through stock speculation? Who knows?
Think about it
Bitmap is converted into drawable and displayed on the screen
[the path of system analyst] Chapter 6: Double inventory demand engineering (comprehensive knowledge concept)
有关计网的五种类型题
Flask blog practice - realize personal center and authority management
【论文阅读|深度】Role-based network embedding via structural features reconstruction with degree-regularized
String longest common prefix
Houdini graphic notes: could not create OpenCL device of type (houdini_ocl_devicetype) problem solving
【RPC】I/O模型——BIO、NIO、AIO及NIO的Rector模式
Sign up to open the third session of the "flying oar hacker marathon". It's been a long time
How to install SSL certificates in Microsoft Exchange 2010
On binary tree


![[RPC] i/o model - Rector mode of bio, NiO, AIO and NiO](/img/29/1945b130c7c6a575f7a56c51136e3a.jpg)






