【leetcode❤python】Binary Watch
#-*- 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的更多相关文章
- 【leetcode❤python】Sum Of Two Number
#-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100 ...
- 【LEETCODE OJ】Binary Tree Postorder Traversal
Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...
- 【leetcode❤python】 111. Minimum Depth of Binary Tree
#-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):# def __init ...
- 【leetcode❤python】 257. Binary Tree Paths
深度优先搜索 # Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# se ...
- 【leetcode❤python】 Maximum Depth of Binary Tree
#-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):# def __init ...
- 【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 ...
- 【leetcode❤python】226. Invert Binary Tree
#-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):# def __init ...
- 【leetcode❤python】110. Balanced Binary Tree
#-*- coding: UTF-8 -*-#平衡二叉树# Definition for a binary tree node.# class TreeNode(object):# def _ ...
- 【leetcode❤python】107. Binary Tree Level Order Traversal II
#-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):# def __init ...
随机推荐
- 夺命雷公狗---微信开发13----获取access_token
获得Access Token的方法1: 这里可以手动进行修改: https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential ...
- libSVM 简易使用手册
关于SVM的基础理论知识,可以google这篇文章<SVM的八股简介>,讲解得生动有趣,是入门的极好教材.作为拿来主义者,我更关心怎么用SVM,因此瞄上了台湾林智仁教授提供的libSVM. ...
- ASP.NET MVC WebApi 返回数据类型序列化控制(json,xml)
我们都知道在使用WebApi的时候Controller会自动将Action的返回值自动进行各种序列化处理(序列化为json,xml等),但是如果Controller的自动序列化后的结果不是我们想要的该 ...
- 小心C# 5.0 中的await and async模式造成的死锁
平时在使用C# 5.0中的await and async关键字的时候总是没注意,直到今天在调试一个ASP.NET项目时,发现在调用一个声明为async的方法后,程序老是莫名其妙的被卡住,就算声明为as ...
- Watir资源列表【转】
Watir简介 "Watir" (发音与 water相近) 全写是 "Web Application Testing in Ruby".Watir是一款用Rub ...
- 上海某(hong)冠笔试题
1.解释Spring的ioc和aop 首先想说说IoC(Inversion of Control,控制倒转).这是spring的核心,贯穿始终.所谓IoC,对于spring框架来说,就是由spring ...
- java总结第四次//常用类
六.常用类 主要内容:Object类.String类.Date类.封装类 (一)Object类 1.Object类是所有Java类的根父类 2.如果在类的声明中未使用extends关键字指明其父类,则 ...
- STM32的寄存器控制SDA_IN()/SDA_OUT()
#define SDA_IN() {GPIOA->CRL&=0X0FFFFFFF;GPIOA->CRL|=(u32)8<<28;}#define SDA_OUT() ...
- linux cache and buffer【转】
转自:http://blog.csdn.net/turkeyzhou/article/details/6426738 版权声明:本文为博主原创文章,未经博主允许不得转载. Linux下对文件的访问和设 ...
- Nagios监控远端的mysql
工作原理: 利用特定的用户定期访问指定的mysql数据库.当不能访问或连不通时则报警. 1.在生产库上安装nagios插件 安装略 备注:编译完显示一定要有mysql支持,不然没有chec ...