class Solution(object):
def readBinaryWatch(self, num):
"""
:type num: int
:rtype: List[str]
"""
hour = [1,2,4,8]
minute = [1,2,4,8,16,32]
self.resHour = {}
self.resMinute = {}
self.res = []
if num < 0 or num >= 9:
return self.res
if num == 0:
return ["0:00"]
for i in range(1,5):
self.generateKHour(hour,i,0,[])
for i in range(1,7):
self.generateKMinute(minute,i,0,[])
self.resHour[0] = [0]
self.resMinute[0] = [0]
for i in range(0,4):
curHour = self.resHour[i]
minuteIndex = num - i
if minuteIndex >= 0 and minuteIndex <= 5:
curMinute = self.resMinute[minuteIndex]
for h in curHour:
for m in curMinute:
if m < 10:
ans = str(h) + ":" + "0" + str(m)
else:
ans = str(h) + ":" + str(m)
self.res.append(ans)
else:
continue
print self.resHour
print self.resMinute
print self.res
return self.res
def generateKHour(self,nums,k,start,ans):
if len(ans) == k:
if sum(ans) >= 0 and sum(ans) <= 11:
if self.resHour.has_key(k):
self.resHour[k].append(sum(ans))
else:
self.resHour[k] = [sum(ans)]
else:
return
for i in range(start,len(nums)):
ans.append(nums[i])
self.generateKHour(nums,k,i+1,ans)
ans.pop()
def generateKMinute(self,nums,k,start,ans):
if len(ans) == k:
if sum(ans) >= 0 and sum(ans) <= 59:
if self.resMinute.has_key(k):
self.resMinute[k].append(sum(ans))
else:
self.resMinute[k] = [sum(ans)]
else:
return
for i in range(start,len(nums)):
ans.append(nums[i])
self.generateKMinute(nums,k,i+1,ans)
ans.pop()
s = Solution()
s.readBinaryWatch(1)