#-*- coding: UTF-8 -*-
from itertools import combinations

class Solution(object):
    
    hourList=[8,4,2,1]
    minList=[32,16,8,4,2,1]
    
    
    def selectHour(self,hourNum):
        if hourNum==0:
            return [0]
        #迭代工具模块包含了操做指定的函数用于操作迭代器
        selectHourList=[]
        hourCombin=list(combinations(self.hourList, hourNum))
        for combine in hourCombin:
            sumT=sum(combine)
            if(sumT<=12):
                selectHourList.append(sumT)

        return selectHourList
    
    def selectMinute(self,minNum):
        if minNum==0:return [0]
        #迭代工具模块包含了操做指定的函数用于操作迭代器
        selectMinList=[]
        
        minCombin=list(combinations(self.minList, minNum))
        for combine in minCombin:
            sumT=sum(combine)
            if(sumT<=59):
                selectMinList.append(sumT)

        return selectMinList
    
    def combinHourMin(self,hourList,minList):

        #直接使用for循环就可以了
        resultsList=[]
        for hour in hourList:
            for min in minList:
 
                minStr=str(min) if len(str(min))>1 else ('0'+str(min))
 
                strTmp=str(hour)+':'+minStr
 
                resultsList.append(strTmp)
     
        return resultsList
    def handleResult(self,resultsList):
        reList=[]
        for item in resultsList:
            for i in item:
                reList.append(i)
        return reList
    
    def readBinaryWatch(self, num):
        if(num>8):
            return
#        elif(num<=0):
#            return '0:00'
        maxHours=3 if num>=3 else num
        maxMinutes=5
        hourLeastNum=0 if (num-maxMinutes)<=0 else (num-maxMinutes)
        
        resultsList=[]

        for hourNum in range(hourLeastNum,maxHours+1):

            selectHourList=self.selectHour(hourNum)
            selectMinList=self.selectMinute(num-hourNum)
            resultsList.append(self.combinHourMin(selectHourList,selectMinList))

        return self.handleResult(resultsList)
    
sol=Solution()
print sol.readBinaryWatch(num=0)
print len(sol.readBinaryWatch(num=0))

【leetcode❤python】Binary Watch的更多相关文章

  1. 【leetcode❤python】Sum Of Two Number

    #-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100 ...

  2. 【LEETCODE OJ】Binary Tree Postorder Traversal

    Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...

  3. 【leetcode❤python】 111. Minimum Depth of Binary Tree

    #-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init ...

  4. 【leetcode❤python】 257. Binary Tree Paths

    深度优先搜索 # Definition for a binary tree node.# class TreeNode:#     def __init__(self, x):#         se ...

  5. 【leetcode❤python】 Maximum Depth of Binary Tree

    #-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init ...

  6. 【leetcode❤python】235. Lowest Common Ancestor of a Binary Search Tree

    #-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init ...

  7. 【leetcode❤python】226. Invert Binary Tree

    #-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init ...

  8. 【leetcode❤python】110. Balanced Binary Tree

    #-*- coding: UTF-8 -*-#平衡二叉树# Definition for a binary tree node.# class TreeNode(object):#     def _ ...

  9. 【leetcode❤python】107. Binary Tree Level Order Traversal II

    #-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init ...

随机推荐

  1. PTPX中的clock tree与LP design

    PTPX在加入CPF/UPF这样的文件后,可以分析multi-voltage,power-gating这样的设计. 针对某个power rail的cell,PTPX支持进行annotate. set_ ...

  2. 【ruby】快速安装gems的方法

    在使用gem install ...的时候增加参数: --no-ri 可以不安装ri部分 --no-rdoc 可以不安装rdoc部分

  3. NOIP201302表达式求值

    NOIP201302表达式求值 题目描述 Description 给定一个只包含加法和乘法的算术表达式,请你编程计算表达式的值. 输入描述 Input Description 输入仅有一行,为需要你计 ...

  4. linux主机vps简单性能测试

    第一,CPU.内存.硬盘检测 cat /proc/cpuinfo (查看CPU信息) cat /proc/meminfo (查看内存信息) df -lh (查看硬盘信息) 这个命令可以看到我们购买的V ...

  5. git-gui

    使用Git.Git GUI和TortoiseGit http://zengrong.net/post/1722.htm 但云桌面不能安装,则TortoiseGit不能使用! 只能想到用totalcmd ...

  6. ESP8266例程

    乐鑫的这个开发板, 可以用LUA来编程, 下面的例子是一个简单的web服务器, 当你用浏览器访问wifi的IP时,后面加一个http://ip/abc123, 这样就可以给wifi模组发命令了. sr ...

  7. PHP 加密的几种方式

    在使用PHP开发Web应用的中,很多的应用都会要求用户注册,而注册的时候就需要我们对用户的信息进行处理了,最常见的莫过于就是邮箱和密码了,本文意在讨论对密码的处理:也就是对密码的加密处理. MD5 相 ...

  8. nginx完美支持tp框架

    nginx完美支持tp框架 server { listen 80; server_name mit.520m.com.cn; access_log /data/wwwlogs/mit.520m.com ...

  9. 温故而知新 clone

    浅复制(浅克隆) 被复制对象所有变量都含有与原来对象的相同值,对象中对其他对象的引用仍然指向原来的对象,换言之,复制对象只复制考虑的对象,而不复制所引用的对象.继承自java.lang.Object类 ...

  10. Django的第一个web程序及深入学习

    本学习历程参照Practical Django Projects和http://djangobook.py3k.cn上翻译的内容进行 注:本例以本机加以说明: 根据Django的安装过程可知:在命令行 ...