作者: 负雪明烛
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)的更多相关文章

  1. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  2. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

  3. 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)

    [LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  4. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

  5. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  6. 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【LeetCode】383. Ransom Note 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  8. 【LeetCode】575. Distribute Candies 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  9. 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...

随机推荐

  1. Python基础笔记3

    高级特性 代码不是越多越好,而是越少越好.代码不是越复杂越好,而是越简单越好.代码越少,开发效率越高. 1.切片 切片(Slice)操作符,取一个list或tuple的部分元素非常常见. 列表 L = ...

  2. R 语言实战-Part 5-1笔记

    R 语言实战(第二版) part 5-1 技能拓展 ----------第19章 使用ggplot2进行高级绘图------------------------- #R的四种图形系统: #①base: ...

  3. 解决sourceforge下载文件慢的方法

    Sourceforge是一些开源软件经常用到的网站,然而国内的网站一直不稳定,如今是可以访问,但是一直无法下载,或者是下载速度慢,导致下载中断 镜像源:http://sourceforge.mirro ...

  4. 为什么重写equals必须重写hashCode

    目录 equals常见面试题 为什么要重写equals 重写equals不重写hashCode会存在什么问题 总结 equals常见面试题 在开始聊之前,我们先看几个常见的面试题,看看你能不能都回答上 ...

  5. 巩固java第四天

    巩固内容: HTML 元素 HTML 文档由 HTML 元素定义. HTML 元素 开始标签 * 元素内容 结束标签 * <p> 这是一个段落 </p> <a href= ...

  6. day07 Linux配置修改

    day07 Linux配置修改 昨日回顾 1.系统目录 /etc :系统配置目录 /bin-> /usr/bin :保存常用命令的目录 /root :超级管理员目录 /home :普通管理员目录 ...

  7. 零基础学习java------day4------流程控制结构

    1. 顺序结构 代码从上往下依次执行 2. 选择结构 也叫分支结构,其会根据执行的结果选择不同的代码执行,有以下两种形式: if  语句 switch  语句 2.1 if 语句 2.1.1  if语 ...

  8. 【swift】Xcode未响应(卡死、卡住、CPU满载、忙碌、转圈圈)

    在尝试了网上的方法,依然没能解决问题,尝试如下: 1.去自己项目的路径,找到<你的项目名.xcodeproj>,点击[显示包内容],删除xcuserdata文件夹 2.去Library,把 ...

  9. 数组实现堆栈——Java实现

    1 package struct; 2 3 4 //接口 5 interface IArrayStack{ 6 //栈的容量 7 int length(); 8 //栈中元素个数(栈大小) 9 int ...

  10. class.getName()和class.getSimpleName()的区别

    根据API中的定义: Class.getName():以String的形式,返回Class对象的"实体"名称: Class.getSimpleName():获取源代码中给出的&qu ...