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

用二进制数字表示的表,小时用4个二进制数表示,分钟用6个二进制数表示,亮的地方代表1,不亮的代表0。给定一个非负整数n,代表现在亮的的个数,问能表示的所有可能时间?

解法1:DFS,遍历每一种可能。

解法2:枚举小时h和分钟m,然后判断二进制1的个数是否等于num。

解法3:位运算(Bit Manipulation)10盏灯泡的燃亮情况可以通过0-1024进行表示,然后计数二进制1的个数即可。利用位运算将状态拆分为小时和分钟。

Java:

public class Solution {
public List<String> readBinaryWatch(int num) {
List<String> list = new ArrayList<>();
int timecode[] = new int[10];
dfs(timecode, 0, 0, list, num);
return list;
} private void dfs(int[] timecode, int i, int k, List<String> list, int num) {
if(k == num) {
String res = decodeToTime(timecode);
if(res != null)
list.add(res);
return;
}
if(i == timecode.length) return;
timecode[i] = 1;
dfs(timecode, i+1, k+1, list, num);
timecode[i] = 0;
dfs(timecode, i+1, k, list, num);
} //输出时间,即输出可能的时间,要是时间不对则输出null
private String decodeToTime(int[] timecode) {
int hours = 0;
//按照位数转换时间
for(int i = 0; i < 4; i++) {
if(timecode[i] == 1) {
hours = hours + (int)Math.pow(2, i);
}
}
int minutes = 0;
for(int i = 4; i < 10; i++) {
if(timecode[i] == 1) {
minutes = minutes + (int)Math.pow(2, i-4);
}
}
String min = "" + minutes;
if(minutes < 10)
min = "0" + min;
//判断时间的可行性
if(hours >= 12 || minutes >= 60)
return null;
return hours + ":" + min;
} }

Python:

class Solution(object):
def readBinaryWatch(self, num):
"""
:type num: int
:rtype: List[str]
"""
ans = []
for h in range(12):
for m in range(60):
if (bin(h)+ bin(m)).count('1') == num:
ans.append('%d:%02d' % (h, m))
return ans

Python:

class Solution(object):
def readBinaryWatch(self, num):
"""
:type num: int
:rtype: List[str]
"""
ans = []
for x in range(1024):
if bin(x).count('1') == num:
h, m = x >> 6, x & 0x3F
if h < 12 and m < 60:
ans.append('%d:%02d' % (h, m))
return ans

  

  

  

[LeetCode] 401. Binary Watch 二进制表的更多相关文章

  1. [LeetCode] Binary Watch 二进制表

    A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom ...

  2. 27. leetcode 401. Binary Watch

    A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom ...

  3. [leetcode] 401. Binary Watch

    https://leetcode.com/contest/5/problems/binary-watch/ 这个题应该是这次最水的,这个题目就是二进制,然后是10位,最大1024,然后遍历,找二进制里 ...

  4. leetcode 338. Counting Bits,剑指offer二进制中1的个数

    leetcode是求当前所有数的二进制中1的个数,剑指offer上是求某一个数二进制中1的个数 https://www.cnblogs.com/grandyang/p/5294255.html 第三种 ...

  5. leetcode 199 :Binary Tree Right Side View

    // 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...

  6. php实现求二进制中1的个数(右移、&、int32位)(n = n & (n - 1);)

    php实现求二进制中1的个数(右移.&.int32位)(n = n & (n - 1);) 一.总结 1.PHP中的位运算符和java和c++一样 2.位移运算符看箭头方向,箭头向左就 ...

  7. 【python】Leetcode每日一题-二叉搜索树节点最小距离

    [python]Leetcode每日一题-二叉搜索树节点最小距离 [题目描述] 给你一个二叉搜索树的根节点 root ,返回 树中任意两不同节点值之间的最小差值 . 示例1: 输入:root = [4 ...

  8. 【python】Leetcode每日一题-二叉搜索迭代器

    [python]Leetcode每日一题-二叉搜索迭代器 [题目描述] 实现一个二叉搜索树迭代器类BSTIterator ,表示一个按中序遍历二叉搜索树(BST)的迭代器: BSTIterator(T ...

  9. 刷题-力扣-剑指 Offer 15. 二进制中1的个数

    剑指 Offer 15. 二进制中1的个数 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/er-jin-zhi-zhong-1de- ...

随机推荐

  1. P4560 [IOI2014]Wall 砖墙

    题目描述 给定一个长度为 nn且初始值全为 00的序列.你需要支持以下两种操作: Add L, R, hL,R,h:将序列 [L, R][L,R]内所有值小于 hh的元素都赋为 hh,此时不改变高度大 ...

  2. 微信之获取微信的openid(二)详细版

    第一步 :配置测试号,网页授权获取用户基本信息. 该授权回掉页面域名为ngrok 映射的域名,我的映射地址是127.0.0.1:8080. 到此微信配置完毕,接下来就是直接上代码了 2.用户同意授权 ...

  3. 【深入ASP.NET原理系列】--Asp.Net Mvc和Asp.Net WebForm实际上共用一套ASP.NET请求管道

    .NET FrameWork4在系统全局配置文件(如在如下目录中C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config) 中添加了一个名字叫Url ...

  4. Mybatis框架-联表查询显示问题解决

    需求:查询结果要求显示用户名,用户密码,用户的角色 因为在用户表中只有用户角色码值,没有对应的名称,角色名称是在码表smbms_role表中,这时我们就需要联表查询了. 这里需要在User实体类中添加 ...

  5. 浏览器报400-Bad Request异常

    今天在使用ie浏览器在测试程序的时候,报这个错误,后台日志打印出来显示的是:连接一个远程主机失败 解决Invalid character found in the request target. Th ...

  6. Python垃圾回收机制?

    Python的GC模块主要运用了“引用计数”(reference counting)来跟踪和回收垃圾.在引用计数的基础上,还可以通过“标记-清除”(mark and sweep)解决容器对象可能产生的 ...

  7. hasura skor 构建安装

    hasura skor 前边有介绍过是一个挺不错的event trigger 插件,我们可以用来进行事件通知处理 官方有提供构建的方法,但是有些还是会有点问题,所以结合构建碰到的问题,修改下 clon ...

  8. 在Matlab中的tick可以调整方向

    需要将axis对话框的More property打开,修改TickDir,可从In改成Out.

  9. Make sure you've included captcha.urls as explained in the INSTALLATION section on

    原因:django-simple-captcha将客户端编号与验证码默认存储在数据库中 解决办法: python manage.py migrate

  10. QHUOJ - 1533: 计算组合数(大数计算)

    题目描述 给定两个正整数n,m,计算组合数C(n,m).组合数计算公式为:C(n,m)=n!/((n-m)!*m!) 已知n,m <= 50. 结果很大需要使用long long存储. 输入 输 ...