Watch binary leetcode-401
Subject description:
Binary watch has four top LED for hour (0-11), six bottom LED for minutes (0-59). Each LED represents a 0 or 1, the least significant bit on the right. Given a non-negative integer n represents the number of lit LED current returns all possible time.
Reference: Negative snow Ming Zhu
from itertools import combinations
class Solution:
def readBinaryWatch(self, num: int) -> List[str]:
def dfs(num,hour,res):
if hour > num: return
for hours in combinations([1,2,4,8],hour):
hs = sum(hours)
if hs >= 12: continue
for mints in combinations([1,2,4,8,16,32],num-hour):
ms = sum(mints)
if ms >= 60: continue
res.append("%d:%02d"%(hs,ms))
dfs(num,hour+1,res)
res = []
dfs(num,0,res)
return res
Reference: Panda love of learning
from itertools import combinations
class Solution:
def readBinaryWatch(self, num: int) -> List[str]:
res = []
for h in range(12):
for m in range(60):
if (bin(h)+bin(m)).count('1') == num:
res.append("%d:%02d"%(h,m))
return res