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