Python (combinations)


  • 0
    J

    Generate all possible combinations of num indexes in the range from 0 to 10. For example, one possible combination of 5 indexes is {0, 1, 4, 7, 8}, which is "3:25".

    def readBinaryWatch(self, num):
        watch = [1,2,4,8,1,2,4,8,16,32]
        times = []
        for leds in itertools.combinations(range(len(watch)), num):
            h = sum(watch[i] for i in leds if i < 4)
            m = sum(watch[i] for i in leds if i >= 4)
            if h > 11 or m > 59: continue
            times.append("{}:{:02d}".format(h, m))
        return times
    

Log in to reply
 

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