当前位置:网站首页>每日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;
}
}
边栏推荐
- 浅谈二叉树
- CDN+COS搭建图床超详细步骤
- Is it safe to open an account through mobile phone if you open an account through stock speculation? Who knows?
- Think about it
- keep-alive
- 什么是 CRA
- 单片机进阶---PCB开发之照葫芦画瓢(二)
- 炒股票开户的话,手机开户安全吗?有谁知道啊?
- CSRF attack
- I hope to explain the basics of canvas as clearly as possible according to my ideas
猜你喜欢
Unreal Engine graphics and text notes: use VAT (vertex animation texture) to make Houdini end on Houdini special effect (ue4/ue5)
報名開啟|飛槳黑客馬拉松第三期如約而至,久等啦
XSS攻击
Complete steps for a complete Oracle uninstall
OpenCV学习(二)---树莓派上安装opencv
无心剑中译伊玛·拉扎罗斯《新巨人·自由女神》
[RPC] i/o model - Rector mode of bio, NiO, AIO and NiO
[image fusion] image fusion based on morphological analysis and sparse representation with matlab code
单片机进阶---PCB开发之照葫芦画瓢(二)
OpenCV学习(一)---环境搭建
随机推荐
Yolov5 changing the upper sampling mode
【OpenCV 例程200篇】210. 绘制直线也会有这么多坑?
I have summarized the knowledge points of JS [intermediate and advanced] for you
Is it safe to speculate in stocks by mobile phone?
Get to know Prometheus
单片机进阶---PCB开发之照葫芦画瓢(二)
Array structure collation
【文件包含漏洞-04】经典面试题:已知某网站仅存在本地文件包含漏洞时,如何GetShell?
Floating window --- create an activity floating window (can be dragged)
【论文阅读|深度】Role-based network embedding via structural features reconstruction with degree-regularized
Is it safe to open a stock account on the compass?
【系统分析师之路】第六章 复盘需求工程(综合知识概念)
Oracle彻底卸载的完整步骤
WPF binding expression and binding data source (I)
Oracle query comes with JDK version
有关计网的五种类型题
New school: no fraud Economics
[paper reading | deep reading] line: large scale information network embedding
无心剑中译伊玛·拉扎罗斯《新巨人·自由女神》
Kotlin arrays and collections (1) {create arrays, use arrays, use for in loops to traverse arrays, use array indexes, and multi-dimensional arrays}