Problem :

https://leetcode.com/problems/binary-watch/


My Solution :

class Solution:
def readBinaryWatch(self, num):
if num == 0:
return ["0:00"]

def convert_to_time(path):
hour = minute = 0
for i in path:
if i < 4:
hour += 2**(3-i)
else:
minute += 2**(9-i)
if hour < 12 and minute < 60:
return '%d:%02d' % (hour, minute)

def dfs(path, i, remain):
if remain == 1:
t = convert_to_time(path+[i])
if t:
ans.append(t)
elif remain <= 10-i:
path.append(i)
for j in range(i+1, 10):
dfs(path, j, remain-1)
path.pop()

ans = []
for i in range(10):
dfs([], i, num)
return ans