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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. [LeetCode&Python] Problem 868. Binary Gap

    Given a positive integer N, find and return the longest distance between two consecutive 1's in the ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. ASP.NET MVC命名空间时引起错误的解决方法

    使用VS2012新建了一个Asp.net mvc5的项目,并把项目的命名空间名称更改了(Src更改为UXXXXX),然后就导致了以下错误 刚开始以后是项目的属性中的命名空间没有更改过来的问题,但我在重 ...

  2. Vscode extensions开发

    Vscode extensions开发   1◆ generatorcode install npm install -g yo generator-code     2◆ 步骤 yo code   ...

  3. ubuntu shell编程笔记

    and 命令 if  [   A  -a   B ] then else fi while [ ] do done set command set  these are parameters $1 s ...

  4. 派生类时使用private的目的 《私有派生》

    第一:继承方式是public的情况下: 当成员是public的时候,派生类对象可以直接调用基类的这个方法和数据, 当数据是private的时候,派生类的对象不能直接调用之,可以通过调用基类的方法来访问 ...

  5. std::string 的方法c_str() 和 data() 有什么区别

    1.从C++标准上的解释来看,只有一点区别: c_str() 返回一个指向正规C字符串的指针常量,该指针保证指向一个 size() + 1 长度的空间,而且最后一个字符肯定是 \0 : 而 data( ...

  6. 【用例管理】用testng的groups管理用例

    一.需求: 测试时经常有两种场景,第一种是冒烟测试的小部分用例:一类是全部用例. 二.针对第一种运行部分的用例,可以用groups来管理 package com.testcases; import o ...

  7. 逆袭之旅DAY17.东软实训.Oracle.PLSQL.过程,函数,包,练习

    2018-07-13 14:54:46 --1.创建一个包,包含一个为雇员加薪的过程,一个为雇员减薪的过程 CREATE OR REPLACE PACKAGE pac_test1 IS PROCEDU ...

  8. POJ 3080 Blue Jeans 后缀数组, 高度数组 难度:1

    题目 http://poj.org/problem?id=3080 题意 有m个(2<=m<=10)不包含空格的字符串,长度为60个字符,求所有字符串中都出现过的最长公共子序列,若该子序列 ...

  9. 实现django admin后台到xadmin后台的转变

    虽然不做前端,还是喜欢好看的东西~.~ 之前同事估计也是功能实现没空管这个后台,前段时间闲的,稍微改了下外貌,前后对比下: Python3.5+Django1.9.7+Xadmin0.6.1 步骤如下 ...

  10. flask-admin fileadmin 上传文件,中文名的解决方案 重写部分secure_filename

    class upload_view(FileAdmin): def _save_form_files(self, directory, path, form): super() filename = ...