#-*- 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. Mysql索引总结(一)

    数据库开发中索引的使用占了很重要的位置,好的索引会使数据库的读写效率加倍,烂的索引则会拖累整个系统甚至引发灾难. 索引分三类: index ----普通的索引,数据可以重复 unique ----唯一 ...

  2. 夺命雷公狗ThinkPHP项目之----企业网站24之网站前台获取当前栏目和顶级栏目

    我们现在要实现的是取出网站当前栏目名称和顶级分类名称,如下所示: 列表页的和单页的不能总是写死的吧?? 我能就要想办法去让他变活的才可以解决问题噢,我们已经有了他的cate_id ,然后我们就可以通过 ...

  3. CSS(Cascading Style Sheet,叠层样式表),作用是美化HTML网页。

    CSS(Cascading Style Sheet,叠层样式表),作用是美化HTML网页. /*注释区域*/    此为注释语法 一.样式表 (一)样式表的分类 1.内联样式表 和HTML联合显示,控 ...

  4. HTML5,超级链接

    <a href="http://h123.date">预算控制系统</a><<br><a href="2.html&quo ...

  5. [v]Debian类系统的有效国内源

    源文件的位置 /etc/apt/sources.list 因为测试需要,装完Debian7 后,更新为163的源,但是后来装软件时,一些软件依赖包还是装不上.后来把163源稍加改动,就好用了.163源 ...

  6. 在IIS站点中Adomd.net集成认证账号问题

    最近在做一个Asp.net项目的时候 ,在C#代码里面用到了Adomd.net去连接SSAS服务器做MDX查询,开发完成后将Asp.net代码部署到IIS后发现Adomd.net老是连接不到SSAS服 ...

  7. Unable to resolve target 'android-19'

    修改两个地方,解决上面的问题

  8. selenium验证码处理

    在爬虫过程中经常遇到验证码,如何处理验证码就显得很重要 现在来说貌似没有完美的解决方案,很多都是通过第三方平台来实现验证码的验证 将获取的验证码的url发送到第三方平台,接收平台返回的验证码,貌似很简 ...

  9. django view使用学习记录

    判断用户是否登录 request.user.is_authenticated()auth.authenticate(username=username, password=password)

  10. [转]学习 WCF (6)--学习调用WCF服务的各种方法

    转自:http://www.cnblogs.com/gaoweipeng/archive/2009/07/26/1528263.html 作者这篇博文写得很全面. 根据不同的情况,我们可以用不同的方法 ...