forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathself-dividing-numbers.py
35 lines (29 loc) · 919 Bytes
/
self-dividing-numbers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Time: O(nlogr) = O(n)
# Space: O(logr) = O(1)
class Solution(object):
def selfDividingNumbers(self, left, right):
"""
:type left: int
:type right: int
:rtype: List[int]
"""
def isDividingNumber(num):
n = num
while n > 0:
n, r = divmod(n, 10)
if r == 0 or (num%r) != 0:
return False
return True
return [num for num in xrange(left, right+1) if isDividingNumber(num)]
# Time: O(nlogr) = O(n)
# Space: O(logr) = O(1)
import itertools
class Solution2(object):
def selfDividingNumbers(self, left, right):
"""
:type left: int
:type right: int
:rtype: List[int]
"""
return [num for num in xrange(left, right+1) \
if not any(itertools.imap(lambda x: int(x) == 0 or num%int(x) != 0, str(num)))]