【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 ...
随机推荐
- 20道C#练习题(一)1——10题
1.输入三个整数,xyz,最终以从小到大的方式输出.利用if嵌套. Console.Write("请输入x="); double x = double.Parse(Console. ...
- Openstack的HA解决方案【haproxy和keepalived】
1. 安装haproxy,keepalived, httpd,3台机器一致. yum install haproxy keepalived httpd -y 2. 修改httpd的默认页面. 在/va ...
- Ul li 横排 菜单
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- mysql datetime设置now()无效,直接用程序设置默认值比较好
mysql datetime设置now()无效的,没有此用法,datetime类型不能设置函数式默认值,只能通过触发器等来搞.想设置默认值,只能使用timestamp类型,然后默认值设置为:CURRE ...
- Atom Remote-FTP connecting FTP with SSL/TLS
{ "protocol": "ftp", "host": "xxx.xxx.xxx.xxx", "port&q ...
- python读入文件
举例说明吧,路径一定要带转义符‘\’,下面的例子中,每一行是一个样本的feature >>> myfile=open("D:\\301\\graduate_thesis\\ ...
- api(接口)文档管理工具
api(接口)文档管理工具 欢迎光临:博之阅API管理平台 ,做为一个app开发者,还没有用到api管理工具,你就OUT了 点击进入:程序员精华博客大全
- Android 仿美团网,大众点评购买框悬浮效果之修改版
转帖请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/17761431),请尊重他人的辛勤劳动成果,谢谢! 我之前写 ...
- JavaEE基础(十一)/Eclipse介绍
1.Java开发工具(常见开发工具介绍) A:操作系统自带的记事本软件 B:高级记事本软件 C:集成开发环境 IDE (Integrated Development Environment) D:Ec ...
- JavaEE基础(三)
1.Java语言基础(逻辑运算符的基本用法) A:逻辑运算符有哪些 &,|,^,! &&,|| B:案例演示 逻辑运算符的基本用法 注意事项: a:逻辑运算符一般用于连接boo ...