作者: 负雪明烛
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. R shinydashboard ——2. 结构

    目录 1.Shiny和HTML 2.结构 3. 标题Header 4. 侧边栏Siderbar 5.主体/正文Body box tabBox infoBox valueBox Layouts 1.Sh ...

  2. Python基础之流程控制if判断

    目录 1. 语法 1.1 if语句 1.2 if...else 1.3 if...elif...else 2. if的嵌套 3. if...else语句的练习 1. 语法 1.1 if语句 最简单的i ...

  3. KEPServeEX 6与KepOPC中间件测试

    KEPServeEX 6可以组态服务器端和客户端连接很多PLC以及具有OPC服务器的设备,以下使用KEPServeEX 6建立一个OPC UA服务器,然后使用KepOPC建立客户端来连接服务器做测试. ...

  4. C#判断是否有中文

    using System.Text.RegularExpressions; Regex reg = new Regex(@"[\u4e00-\u9fa5]"); if (reg.I ...

  5. 4 — springboot中的jsr303检验

    1.导入依赖 <!--JSR303校验的依赖 --> <dependency> <groupId>org.springframework.boot</grou ...

  6. 日常Java 2021/10/4

    读取控制台输入 将System.in包装在BufferedReader对象中来创建一个字符流 BufferedReader b = new BufferedReader(new InputStream ...

  7. Spark On Yarn的各种Bug

    今天将代码以Spark On Yarn Cluster的方式提交,遇到了很多很多问题.特地记录一下. 代码通过--master yarn-client提交是没有问题的,但是通过--master yar ...

  8. 令无数程序员加班的 Log4j2 远程执行漏洞复现

    前情提要 Apache 存在 Log4j 远程代码执行漏洞,将给相关企业带来哪些影响?还有哪些信息值得关注? 构建maven项目引入Log4j2 编写 pom 文件 <?xml version= ...

  9. 03 - Vue3 UI Framework - 首页

    顶部边栏做完了,接下来开始做官网的首页 返回阅读列表点击 这里 创建视图文件夹 让我们先新建一个 src/views 文件夹,用来存放官网的主要视图 然后在该文件夹下新建两个 vue 文件,作为我们的 ...

  10. LeetCode 36. Valid Sudoku (Medium)

    题目 Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according ...