【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 ...
随机推荐
- linux进程自动关闭与dmesg的使用
一些应用程序,后台服务被关掉.例如内存不足等,可能是操作系统关掉的.这些日志记录在dmesg中. 存储目录:/var/log/dmesg dmesg -T 可以将时间戳转化为可以识别的时间. | he ...
- ASP.NET MVC (一)
工作清闲好一段时间了,趁这段时间弄了弄PHP,做个了简单的MVC网页.玩了玩Android,弄了个拨号器,发短信的,嘿嘿,最满意的还是两天弄了个数独游戏.不务正业一个多月了,也该磨磨刀,接下来一段时间 ...
- 【ruby】ruby基础知识
Install Ruby(安装) For windows you can download Ruby from http://rubyforge.org/frs/?group_id=167 for L ...
- UIActionSheet和UIAlert
UIActionSheet: 首先,在.h文件中添加Protocol,(Protocol相当于Java中的interface) @interface ActionSheetViewController ...
- LR 常见问题收集及总结
一:LoadRunner常见问题整理 1.LR 脚本为空的解决方法: 1.去掉ie设置中的第三方支持取消掉 2.在系统属性-高级-性能-数据执行保护中,添加loadrunner安装目录中的vugen. ...
- grads 读取浓度值
- python读入文件
举例说明吧,路径一定要带转义符‘\’,下面的例子中,每一行是一个样本的feature >>> myfile=open("D:\\301\\graduate_thesis\\ ...
- array DEMO
[xiluhua@vm-xiluhua][~]$ declare -a array #申明数组 [xiluhua@vm-xiluhua][~]$ array=("a" " ...
- PHP笔记随笔
1.CSS控制页面文字不能复制: body{-webkit-user-select:none;} 2.[php过滤汉字和非汉字] $sc="aaad....##--__i汉字过滤&quo ...
- HDU 5795:A Simple Nim(博弈)
http://acm.hdu.edu.cn/showproblem.php?pid=5795 A Simple Nim Problem Description Two players take t ...