【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 ...
随机推荐
- Javascript中函数及变量定义的提升
<html> <head> <title>函数提升</title> <script language="javascript" ...
- Open quote is expected for attribute "property" associated with an element type "result".错误
java Mybatis 框架下的项目 报 Open quote is expected for attribute "property" associated with a ...
- Java中的get()和set()方法
对于JAVA初学者来说,set和get这两个方法似乎已经很熟悉了,这两个方法是JAVA变成中的基本用法,也是出现频率相当高的两个方法. 如果你对于这两个方法还有困惑甚至完全不知道这两个方法是做什么的, ...
- Android判断网络状态
package com.ch.services; import com.ch.utils.NetWorkUtils; import android.app.Service; import androi ...
- 改Bug总结
[1]屏蔽取舍法 屏蔽取舍,即所谓与问题无关的前后“语境”完全可以忽略,首先屏蔽掉,再根据问题复现路径查看问题发生的区间,然后逐近锁定“病灶”,确定需要修改的目标. [2]追溯原形法 追溯原形,即需要 ...
- android 学习随笔十四(页面跳转与数据传递)
1.activity 创建第二个Activity 需要在清单文件中为其配置一个activity标签 标签中如果带有这个子节点,则会在系统中多创建一个快捷图标 <intent-filter> ...
- Javascript 类与静态类的实现-js面向对象
在Javascript里,对面向对象并没有一个直接的实现,对于代码方面也是非常的灵活. 今天所要说的就是,如何在Javascript里写类与静态类,这是本人一惯用的方法,你也可以有更为方便的,也可以发 ...
- linux设备驱动归纳总结(七):1.时间管理与内核延时【转】
本文转载自:http://blog.chinaunix.net/uid-25014876-id-100005.html linux设备驱动归纳总结(七):1.时间管理与内核延时 xxxxxxxxxxx ...
- IBatisNet Demo (升级from 1.1)
sqlMap.config, 要修改provider的设置 <providers resource="providers.config"/> <database& ...
- org.apache.commons.httpclient
org.apache.commons.httpclient /** * post 方法 * @param url * @param params * @return */ public static ...