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. django项目使用layui插件给网站设置一个日历挂件,很简单实用。

    进入https://www.layui.com/首页下载layui文件 下载解压后把文件放在static静态文件中, html页面引入css和js <link rel="stylesh ...

  2. Educational Codeforces Round 64 (Rated for Div. 2)题解

    Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...

  3. python测试开发django-rest-framework-65.序列化(ModelSerializer)

    前言 serializers.Serializer可以对modle模型中的字段序列化,并且必须写create和update两个方法.ModelSerializer可以看成是Serializer的一个升 ...

  4. Winform异常处理之ThreadException、unhandledException及多线程异常处理

    异常处理之ThreadException.unhandledException及多线程异常处理 一:ThreadException和unhandledException的区别 处理未捕获的异常是每个应 ...

  5. Hive UDF函数构建

    1. 概述 UDF函数其实就是一个简单的函数,执行过程就是在Hive转换成MapReduce程序后,执行java方法,类似于像MapReduce执行过程中加入一个插件,方便扩展.UDF只能实现一进一出 ...

  6. 小米BL不解锁刷机

    关于小米NOTE顶配近期解锁的问题中发现还有很多人不会用9008模式刷机,现出个简单教程方便米粉们救砖.硬件:小米NOTE顶配手机 win10系统的电脑 手机与电脑相连的数据线软件:老版本的mifla ...

  7. B-树,B+树,B*树总结

    链接地址:https://blog.csdn.net/v_JULY_v/article/details/6530142 B+树 B+ 树是一种树数据结构,是一个n叉树,每个节点通常有多个孩子,一棵B+ ...

  8. VISIO 2016安装破解

    VISIO 2016安装破解 安装包下载 (2.41G) 安装包下载地址 :https://msdn.itellyou.cn/ ed2k://|file|cn_visio_professional_2 ...

  9. MySQL 详细解读undo log :insert undo,update undo

    转自aobao.org/monthly/2015/04/01/ 本文是对整个Undo生命周期过程的阐述,代码分析基于当前最新的MySQL5.7版本.本文也可以作为了解整个Undo模块的代码导读.由于涉 ...

  10. Windows用户模式调试内部组件

    简介 允许用户模式调试工作的内部机制很少得到充分的解释.更糟糕的是,这些机制在Windows XP中已经发生了根本性的变化,当许多支持被重新编写时,还通过将ntdll中的大多数例程作为本地API的一部 ...