10-line clean Python code using itertools, beating 90%


  • 0
    J
    class Solution(object):
        def readBinaryWatch(self, num):
            """
            :type num: int
            :rtype: List[str]
            """
            
            leds = [8, 4, 2, 1, 32, 16, 8, 4, 2, 1]
            res = []
    
            indices = itertools.combinations(range(10), num)
            for index in indices:
                hour = sum([leds[i] for i in index if i < 4])
                minute = sum([leds[i] for i in index if i > 3])
                if hour > 11 or minute > 59:
                    continue
                res.append('{}:{:02d}'.format(hour, minute))
            return res
    

Log in to reply
 

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