python - binary watch - 95%


  • 0
    S
    class Solution(object):
        def readBinaryWatch(self, num):
            """
            :type num: int
            :rtype: List[str]
            """
            if num > 8:
                return []
            
            d = {}
            for s in range(8+1):
                for i in range(s+1):
                    if i <= 3 and s-i <= 5:
                        try:
                            d[s].append((i,s-i))
                        except:
                            d[s] = [(i,s-i)]
               
                    
            dd = {}
            for i in range(59+1):
                try:
                    dd[bin(i).count('1')].append(i)
                except:
                    dd[bin(i).count('1')] = [i]
            
            ans = []
            for t in d[num]:
                for h in dd[t[0]]:
                    if h < 12:
                        for m in dd[t[1]]:
                            s = str(str(h) + ':' + ('0' if m < 10 else '') + str(m))
                            ans.append(s)
            
            return ans
                    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.