首页 > 其他 > 详细

1346. Check If N and Its Double Exist

时间:2020-07-15 00:58:49      阅读:47      评论:0      收藏:0      [点我收藏+]

Given an array arr of integers, check if there exists two integers N and M such that N is the double of M ( i.e. N = 2 * M).

More formally check if there exists two indices i and j such that :

  • i != j
  • 0 <= i, j < arr.length
  • arr[i] == 2 * arr[j]

给一个数组,求是否存在两个数n和m是的n=2*m

一个hashtable解决了吧

class Solution(object):
    def checkIfExist(self, arr):
        """
        :type arr: List[int]
        :rtype: bool
        """
        d = {}
        for value in arr:
            if value * 2 in d:
                return True
            elif value % 2 == 0 and value // 2 in d:
                return True
            d[value] = 1
        return False

 

1346. Check If N and Its Double Exist

原文:https://www.cnblogs.com/whatyouthink/p/13302209.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!