作者: 负雪明烛
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. Session和Cookie的原理,以及在分布式应用中出现的问题和解决方案

    产生原因 由于http协议是无状态的,同一个浏览器对服务器的两次请求之间是没有关系的,服务器认为两次请求都是全新的请求,不会记住上次请求成功的数据.然而现有的业务常常需要服务器能记住用户的访问情况, ...

  2. 45-Letter Combinations of a Phone Number

    Letter Combinations of a Phone Number My Submissions QuestionEditorial Solution Total Accepted: 7855 ...

  3. Windows Server 2016域控制器升级到Windows Server 2022遇到的问题记录Fix error 0x800F081E – 0x20003

    1. 非域控服务器升级 将两台Web服务器和数据库服务器(Windows Server 2016, 2019)成功升级至到Windows Server 2022,非常顺利,一次成功. 直接在Windo ...

  4. Fragment放置后台很久(Home键退出很长时间),返回时出现Fragment重叠解决方案

    后来在google查到相关资料,原因是:当Fragment长久不使用,系统进行回收,FragmentActivity调用onSaveInstanceState保存Fragment对象.很长时间后,再次 ...

  5. Android 开源框架Universal-Image-Loader加载https图片

    解决方案就是 需要 android https HttpsURLConnection 这个类忽略证书 1,找到 Universal-Image-Loader的library依赖包下面com.nostr ...

  6. ORACLE 本session产生的redo

    select * from v$statname a ,v$mystat bwhere a.STATISTIC# = b.STATISTIC# and a.name = 'redo size';

  7. tableView和tableViewCell的背景颜色问题

    当在tableView中添加cell数据时,我们会发现原本设置的tableView的背景颜色不见了,这是因为加载cell数据时,tableView的背景颜色被cell数据遮盖住了,此时,可以通过设置c ...

  8. spring注解-bean生命周期

    https://www.jianshu.com/p/70b935f2b3fe bean的生命周期 bean创建---初始化----销毁的过程 容器管理bean的生命周期 对象创建:容器启动后调用bea ...

  9. 页面屏蔽backspace键

    1 //页面加载完成 2 $(document).ready(function(){ 3 //禁止退格键 作用于Firefox.Opera 4 document.onkeypress = banBac ...

  10. matplotlib画3d图

    import numpy as npimport matplotlib.pyplot as pltfrom mpl_toolkits.mplot3d import Axes3D fig = plt.f ...