Python solution using itertools.combination


  • 0
    U
    class Solution(object):
        def readBinaryWatch(self, num):
            """
            :type num: int
            :rtype: List[str]
            """
            if num > 10 or num < 0:
                return []
            soln = []
            for comb in itertools.combinations(range(10), num):
            #let comb be the locations of '1's
                h = int("".join(['1' if i in comb
                                 else '0'
                                 for i in range(4)]), 2)
                m = int("".join(['1' if i in comb
                                 else '0'
                                 for i in range(4, 10)]), 2)
                if h < 12 and m < 60:
                    soln.append(str(h) + ":" + str(m).zfill(2))
            return soln
    

Log in to reply
 

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