forked from Srinivas11789/AlgorithmNuggets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbin.py
More file actions
18 lines (16 loc) · 710 Bytes
/
bin.py
File metadata and controls
18 lines (16 loc) · 710 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution(object):
def readBinaryWatch(self, num):
"""
:type num: int
:rtype: List[str]
"""
# The binary watch <look at the example>, for each n, the number of ones in the hours or minutes or hours+minutes is n. Hence for each n, find the number of ones in the all the combinations
result = []
# hours is 0-11 => 12
for i in range(12):
# minutes is 0-59 => 60
for j in range(60):
if (bin(i)+bin(j)).count("1") == num:
# construct the time such that two zeros appears after the colon
result.append("%d:%02d" % (i,j))
return result