Skip to content

Commit d2f5f9e

Browse files
authored
Create check-if-the-number-is-fascinating.py
1 parent b47b1fc commit d2f5f9e

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Time: O(logn)
2+
# Space: O(1)
3+
4+
# string, bitmask
5+
class Solution(object):
6+
def isFascinating(self, n):
7+
"""
8+
:type n: int
9+
:rtype: bool
10+
"""
11+
lookup = [0]
12+
def check(x):
13+
while x:
14+
x, d = divmod(x, 10)
15+
if d == 0 or lookup[0]&(1<<d):
16+
return False
17+
lookup[0] |= (1<<d)
18+
return True
19+
20+
return check(n) and check(2*n) and check(3*n)
21+
22+
23+
# Time: O(logn)
24+
# Space: O(logn)
25+
# string
26+
class Solution2(object):
27+
def isFascinating(self, n):
28+
"""
29+
:type n: int
30+
:rtype: bool
31+
"""
32+
s = str(n)+str(2*n)+str(3*n)
33+
return '0' not in s and len(s) == 9 and len(set(s)) == 9

0 commit comments

Comments
 (0)