Easy Understand 46ms Python Solution With HashTable


  • 0
    S
    class Solution(object):
        def readBinaryWatch(self, num):
            """
            :type num: int
            :rtype: List[str]
            """
            up = 11
            down = 59
            up_hash = self.generateHash(up)
            down_hash = self.generateHash(down)
            n = num
            result = []
            while n >=0:
            	rest = num - n
            	hours = up_hash[n]
            	minutes = down_hash[rest]
            	for hour in hours:
            		for minute in minutes:
            			result.append('%d:%.2d'%(hour,minute))
            	n = n - 1
            return result
    
        def generateHash(self, num):
        	num_hash = collections.defaultdict(list)
            while num >= 0:
            	n = num
            	count = 0
            	while n > 0:
            		count = count + (n & 1)
            		n = n >> 1
            	num_hash[count].append(num)
            	num = num - 1
            return num_hash
    

Log in to reply
 

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