给你一个整数数组 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 。
true
说明存在,返回 false
说明不存在;package Array;
/**
* Created with IntelliJ IDEA.
* Version : 1.0
* Author : cunyu
* Email : cunyu1024@foxmail.com
* Website : https://cunyu1943.github.io
* 公众号 : 村雨遥
* Date : 2020/4/28 下午3:06
* Project : LeetCode
* Package : Array
* Class : OneThreeFourSix
* Desc : 1346. 检查整数及其两倍数是否存在
*/
public class OneThreeFourSix {
public static void main(String[] args) throws Exception {
int[] arr = {10, 2, 5, 3};
System.out.println(checkIfExist(arr));
}
/**
* 1346. 检查整数及其两倍数是否存在
*
* @param arr 待检查的数组
* @return true 整数及其两倍数存在; false 整数及其两倍数不存在
*/
public static boolean checkIfExist(int[] arr) {
// 遍历数组两次,当两次索引位置不同时,判断当前整数及其两倍数是否存在
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
// 两次索引位置不同
if (i != j) {
// 判断整数及其两倍数是否存在
if (arr[i] * 2 == arr[j]) {
return true;
}
}
}
}
return false;
}
}
❝文章会优先发布在 「公众号」 和 「知乎」,欢迎关注; 公众号:「村雨遥」 知乎:https://www.zhihu.com/people/cunyu1943 个人博客:https://cunyu1943.github.io Github:https://github.com/cunyu1943 CSDN:https://blog.csdn.net/github_39655029 简书:https://www.jianshu.com/u/9fd25635ae22 Gitbook:https://cunyu1943.gitbook.io 腾讯云社区:https://cloud.tencent.com/developer/user/6209990 ❞
[1]
1346. 检查整数及其两倍数是否存在: https://leetcode-cn.com/problems/check-if-n-and-its-double-exist/
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有