1346. Check If N and Its Double Exist

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

 

posted @ 2020-07-14 22:16  whatyouthink  阅读(95)  评论(0编辑  收藏  举报