python without using dfs but conbinations


  • 0
    Q
    class Solution(object):
        def readBinaryWatch(self, num):
            """
            :type num: int
            :rtype: List[str]
            """
            List = []
            if num > 8:
                return List
            if num < 1:
                List.append("0:00")
                return List
                
            min_hour = (num-5) if (num-5) > 0 else 0
            max_hour = num if num < 3 else 3      
            min_minute = (num-3) if (num-3) > 0 else 0
            max_minute = num if num <5 else 5
            
            from itertools import combinations 
            for i in range(min_hour,max_hour+1):
                #组合
                for h in list(combinations([1,2,4,8],i)):
                    total_h = sum(h)
                    if total_h > 11:
                        continue
                    for m in list(combinations([1,2,4,8,16,32], num-i)):
                        total_m = sum(m)
                        if total_m > 59:
                            continue
                        
                        total_h = str(total_h)
                        total_m = ('0'+str(total_m)) if total_m < 10 else str(total_m)
                            
                        List.append(total_h+':'+total_m)
            return List
    

  • 0
    Q
    This post is deleted!

Log in to reply
 

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