-
Notifications
You must be signed in to change notification settings - Fork 43
/
binary-watch.py
118 lines (109 loc) · 3.32 KB
/
binary-watch.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# V0
# V1
# https://www.jianshu.com/p/7723d729da3b
# IDEA : GREEDY
class Solution(object):
def readBinaryWatch(self, num):
return ['%d:%02d' % (h, m)
for h in range(12) for m in range(60)
if (bin(h) + bin(m)).count('1') == num]
# V1'
# https://www.jianshu.com/p/7723d729da3b
# IDEA : HASH MAP + GREEDY
class Solution(object):
# Binary Watch
def readBinaryWatch(self, num):
"""
:type num: int
:rtype: List[str]
"""
hourMap = {i: self.countOfBits(i) for i in range(12)}
minutesMap = {i: self.countOfBits(i) for i in range(60)}
res = []
for hour in filter(lambda x: hourMap[x] <= num, range(12)):
res += ["%s" % hour + ":" + ("%s" % minute).zfill(2) for minute in filter(lambda x: minutesMap[x] == num - hourMap[hour], range(60))]
return res
def countOfBits(self, num):
counter = 0
while num:
counter += 1
num &= num - 1
return counter
# V1''
# https://www.jianshu.com/p/7723d729da3b
# IDEA : MATH
class Solution(object):
def readBinaryWatch(self, num):
"""
:type num: int
:rtype: List[str]
"""
res = []
if num > 10 or num < 0:
return res
for comb in itertools.combinations(range(10), num):
h = int("".join(['1' if i in comb
else '0'
for i in range(4)]), 2)
m = int("".join(['1' if i in comb
else '0'
for i in range(4, 10)]), 2)
if h < 12 and m < 60:
res.append(str(h) + ":" + str(m).zfill(2))
return res
# V1'''
# http://bookshadow.com/weblog/2016/09/18/leetcode-binary-watch/
# IDEA : BIT MANIPULATION
class Solution(object):
def readBinaryWatch(self, num):
"""
:type num: int
:rtype: List[str]
"""
ans = []
for x in range(1024):
if bin(x).count('1') == num:
h, m = x >> 6, x & 0x3F
if h < 12 and m < 60:
ans.append('%d:%02d' % (h, m))
return ans
# V1''''
# http://bookshadow.com/weblog/2016/09/18/leetcode-binary-watch/
# IDEA : GREEDY
class Solution(object):
def readBinaryWatch(self, num):
"""
:type num: int
:rtype: List[str]
"""
ans = []
for h in range(12):
for m in range(60):
if (bin(h)+ bin(m)).count('1') == num:
ans.append('%d:%02d' % (h, m))
return ans
# V2
# Time: O(1)
# Space: O(1)
class Solution(object):
def readBinaryWatch(self, num):
"""
:type num: int
:rtype: List[str]
"""
def bit_count(bits):
count = 0
while bits:
bits &= bits-1
count += 1
return count
return ['%d:%02d' % (h, m) for h in range(12) for m in range(60)
if bit_count(h) + bit_count(m) == num]
def readBinaryWatch2(self, num):
"""
:type num: int
:rtype: List[str]
"""
return ['{0}:{1}'.format(str(h), str(m).zfill(2))
for h in range(12) for m in range(60)
if (bin(h) + bin(m)).count('1') == num]