【LeetCode】401. Binary Watch 解题报告(Java & Python)
作者: 负雪明烛
 id: fuxuemingzhu
 个人博客: http://fuxuemingzhu.cn/
[LeetCode]
题目地址:https://leetcode.com/problems/binary-watch/
- Difficulty: Easy
题目描述
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”.
题目大意
有个二进制手表,求亮n个灯的时候,能显示多少种时间?
解题方法
java解法
尝试暴力解决。看12小时内的哪个一分钟的二进制表示值等于题目给的num,效率不太高。
public class Solution {
    public List<String> readBinaryWatch(int num) {
        ArrayList<String> times = new ArrayList<String>();
        for(int h =0; h<12; h++){
            for(int m=0; m<60; m++){
                if(Integer.bitCount(h*64 + m) == num){
                    times.add(String.format("%d:%02d", h, m));
                }
            }
        }
        return times;
    }
}
AC: 34 ms 超过14.87%
----更新----
Python解法
还是使用回溯法。这个题的回溯法其实就是枚举小时亮灯数和分钟亮灯数。
知道小时的灯的亮的个数需要使用python的combinations进行一次组合运算,才能遍历所有的小时情况。
另外就是要注意,小时和分钟这两个循环是嵌套的。
题目中给的时间的范围是0-11小时和0-59分钟,越界判断也要注意。
from itertools import combinations
class Solution(object):
    def readBinaryWatch(self, num):
        """
        :type num: int
        :rtype: List[str]
        """
        res = []
        self.dfs(num, 0, res)
        return res
    def dfs(self, num, hours, res):
        if hours > num : return
        for hour in combinations([1, 2, 4, 8], hours):
            hs = sum(hour)
            if hs >= 12 : continue
            for minu in combinations([1, 2, 4, 8, 16, 32], num - hours):
                mins = sum(minu)
                if mins >= 60 : continue
                res.append("%d:%02d" % (hs, mins))
        self.dfs(num, hours + 1, res)
日期
2017 年 1 月 11 日
 2018 年 2 月 24 日
 2018 年 11 月 17 日 —— 美妙的周末,美丽的天气
【LeetCode】401. Binary Watch 解题报告(Java & Python)的更多相关文章
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
		[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ... 
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
		[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ... 
- 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)
		[LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ... 
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
		[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ... 
- 【LeetCode】120. Triangle 解题报告(Python)
		[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ... 
- 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)
		作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ... 
- 【LeetCode】383. Ransom Note 解题报告(Java & Python)
		作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ... 
- 【LeetCode】575. Distribute Candies 解题报告(Java & Python)
		作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ... 
- 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)
		作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ... 
随机推荐
- Assemblytics鉴定基因组间SV
			Assemblytics, 发表在Bioinformaticshttp://www.ncbi.nlm.nih.gov/pubmed/27318204,鉴定基因组间SV. Githup,https:// ... 
- android listview展示图片
			最近学习android开发,感触颇多,和网站开发对比,还是有很大的差距,在这里记录一下. android listview展示图片 在网站开发上,展示图片非常简单,一个HTML img标签就搞定,加上 ... 
- 34、在排序数组中查找元素的第一个和最后一个位置 | 算法(leetode,附思维导图 + 全部解法)300题
			零 标题:算法(leetode,附思维导图 + 全部解法)300题之(34)在排序数组中查找元素的第一个和最后一个位置 一 题目描述 二 解法总览(思维导图) 三 全部解法 1 方案1 1)代码: / ... 
- JavaBean内省与BeanInfo
			Java的BeanInfo在工作中并不怎么用到,我也是在学习spring源码的时候,发现SpringBoot启动时候会设置一个属叫"spring.beaninfo.ignore", ... 
- Slay 全场!Erda 首次亮相 GopherChina 大会
			来源|尔达 Erda 公众号 相关视频:https://www.bilibili.com/video/BV1MV411x7Gm 2021 年 6 月 26 日,GopherChina 大会准时亮相北京 ... 
- [云原生]Docker - 安装&卸载
			目录 系统要求 卸载旧版本 安装Docker 方法一:通过repo安装 设置Repository 安装Docker Engine 升级Docker Engine 方法二:通过package安装 方法三 ... 
- Flume(四)【配置文件总结】
			目录 一.Agent 二.Source taildir arvo netstat exec spooldir 三.Sink hdfs kafka(待续) hbase(待续) arvo logger 本 ... 
- map/multimap深度探索
			map/multimap同样以rb_tree为底层结构,同样有元素自动排序的特性,排序的依据为key. 我们无法通过迭代器来更改map/multimap的key值,这个并不是因为rb_tree不允许, ... 
- Shell学习(八)——dd命令
			一.dd命令的解释 dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. 注意:指定数字的地方若以下列字符结尾,则乘以相应的数字:b=512:c=1:k=1024:w=2 参数注释: 1. ... 
- Linux系统中安装软件方法总结
			Linux系统中安装软件方法总结 [1]Linux系统中安装软件的几种方式 [2] Linux配置yum源(本地源和网络源) [3] SuSE下zypper源配置 [4] SUSE zypper 本地 ... 
