[LeetCode&Python] Problem 401. Binary Watch
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59).
Each LED represents a zero or one, with the least significant bit on the right.
For example, the above binary watch reads "3:25".
Given a non-negative integer n which represents the number of LEDs that are currently on, return all possible times the watch could represent.
Example:
Input: n = 1
Return: ["1:00", "2:00", "4:00", "8:00", "0:01", "0:02", "0:04", "0:08", "0:16", "0:32"]
Note:
- The order of output does not matter.
- The hour must not contain a leading zero, for example "01:00" is not valid, it should be "1:00".
- The minute must be consist of two digits and may contain a leading zero, for example "10:2" is not valid, it should be "10:02".
class Solution(object):
def readBinaryWatch(self, num):
"""
:type num: int
:rtype: List[str]
"""
ans=[]
def findans(leds,bits=0,i=0):
if bits==num:
h,m=int(''.join(leds[:4]),2), int(''.join(leds[4:]),2)
if h<12 and m<60:
ans.append("{}:{:02d}".format(h,m))
return
for j in range(i,10):
leds[j]='1'
findans(leds,bits+1,j+1)
leds[j]='0'
findans(['0']*10)
return ans
[LeetCode&Python] Problem 401. Binary Watch的更多相关文章
- [LeetCode&Python] Problem 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...
- [LeetCode&Python] Problem 107. Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- [LeetCode&Python] Problem 563. Binary Tree Tilt
Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the ab ...
- [LeetCode&Python] Problem 693. Binary Number with Alternating Bits
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...
- [LeetCode&Python] Problem 868. Binary Gap
Given a positive integer N, find and return the longest distance between two consecutive 1's in the ...
- [LeetCode&Python] Problem 704. Binary Search
Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...
- [LeetCode&Python] Problem 108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...
- [LeetCode&Python] Problem 226. Invert Binary Tree
Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1 Tr ...
- [LeetCode&Python] Problem 235. Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
随机推荐
- WannaCry(永恒之蓝)病毒处理方法
1.直接关闭server服务 打开cmd执行关闭server服务即可: net stop server 控制面板--管理工具--服务里手动关掉 2.防火墙限制445端口 3.打补丁 [KB401259 ...
- jcmd
1.jps 2.jcmd 1761[pid] PerfCounter.print 查看指定进程的性能统计信息 概述 在JDK1.7以后,新增了一个命令行工具 jcmd.他是一个多功能的工具,可以用它来 ...
- jstack Dump 日志文件中的线程状态
[转]jstack Dump 日志文件中的线程状态 dump 文件里,值得关注的线程状态有: 死锁,Deadlock(重点关注) 执行中,Runnable 等待资源,Waiting on condi ...
- QPainter绘制渐进色文本
参考资料:https://blog.csdn.net/what951006/article/details/52876513 效果图: 代码: void WgtText::paintEvent(QPa ...
- python 自然语言处理(五)____WordNet
WordNet是面向语义的英语词典,与传统辞典类似,但结构更丰富.nltk中包括英语WordNet,共有155287个单词和117659个同义词. 1.寻找同义词 这里以motorcar为例,寻找它的 ...
- javascript 数组函数
声明数组 var tmp=[];//简写模式 var tmp= new Array();//直接new一个 var tmp=array(); //直接new一个 在new数组的时候可以传入一个参数,表 ...
- javaweb环境搭建
首先,在开始搭建MyEclipse的开发环境之前,还有三步工具的安装需要完成,只要在安装配置成功之后才可以进入下面的java Web项目开发环境的搭建. 1.安装工具 第一步,下载并安装JDK,到官网 ...
- CSS(三)--自定义标签
HTML代码 <body> <ul> <li>1</li> <li>2</li> </ul> </body&g ...
- flask上传下载文件(一)下载
简介: 作为一个可以和用户交互的web应用,必然要有数据导出功能,导出到excel是比较常用的方式. flask有一个扩展叫flask-excel,可能不适合中国人用,因为没有看到修改列名的功能.也许 ...
- asp.net MVC之Result过滤器浅析
在asp.net MVC中,每一个Action方法完成之后都会返回一个结果,而我们可以在Result过滤器中根据需要修改这个结果.例如可以根据UserAgent来判断客户端的来源是手机还是PC端,从而 ...