-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0401_binary_watch.py
More file actions
32 lines (27 loc) · 901 Bytes
/
0401_binary_watch.py
File metadata and controls
32 lines (27 loc) · 901 Bytes
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
class Solution(object):
def readBinaryWatch(self, num):
"""
:type num: int
:rtype: List[str]
>> readBinaryWatch(0)
["0:00"]
"""
# Hours (0-11), Minutes (0-59)
if (num > 8 or num < 0):
return []
elif (num == 0):
return ["0:00"]
time = []
# Hour
for i in range(12):
# Minute
for j in range (60):
# Count total '1's from bin(i) and bin(j)
if (bin(i).count('1') + bin(j).count('1') == num):
# Add leading zero to the minute
if (j < 10):
time.append(str(i) + ":0" + str(j))
# No leading zero
else:
time.append(str(i) + ":" + str(j))
return time