[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 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 二进制表的更多相关文章
- [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 ...
- 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 ...
- [leetcode] 401. Binary Watch
https://leetcode.com/contest/5/problems/binary-watch/ 这个题应该是这次最水的,这个题目就是二进制,然后是10位,最大1024,然后遍历,找二进制里 ...
- leetcode 338. Counting Bits,剑指offer二进制中1的个数
leetcode是求当前所有数的二进制中1的个数,剑指offer上是求某一个数二进制中1的个数 https://www.cnblogs.com/grandyang/p/5294255.html 第三种 ...
- leetcode 199 :Binary Tree Right Side View
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
- php实现求二进制中1的个数(右移、&、int32位)(n = n & (n - 1);)
php实现求二进制中1的个数(右移.&.int32位)(n = n & (n - 1);) 一.总结 1.PHP中的位运算符和java和c++一样 2.位移运算符看箭头方向,箭头向左就 ...
- 【python】Leetcode每日一题-二叉搜索树节点最小距离
[python]Leetcode每日一题-二叉搜索树节点最小距离 [题目描述] 给你一个二叉搜索树的根节点 root ,返回 树中任意两不同节点值之间的最小差值 . 示例1: 输入:root = [4 ...
- 【python】Leetcode每日一题-二叉搜索迭代器
[python]Leetcode每日一题-二叉搜索迭代器 [题目描述] 实现一个二叉搜索树迭代器类BSTIterator ,表示一个按中序遍历二叉搜索树(BST)的迭代器: BSTIterator(T ...
- 刷题-力扣-剑指 Offer 15. 二进制中1的个数
剑指 Offer 15. 二进制中1的个数 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/er-jin-zhi-zhong-1de- ...
随机推荐
- debug版本的DLL调用release版本的DLL引发的一个问题
stl的常用结构有 vector.list.map等. 今天碰到需要在不同dll间传递这些类型的参数,以void*作为转换参数. 比如 DLL2 的接口 add(void*pVoid); 1.在DLL ...
- 如何在Windows上部署Redis集群和SpringBoot进行整合
一.安装Redis的Windows版本并进行配置 (1)下载链接 https://github.com/microsoftarchive/redis/releases (2)将下载后的Redis复制成 ...
- [HTML5] Using HTMLPortalElement to improve MPA preformance
For multi pages application, it is very slow to navgiate between page by page, because it needs to r ...
- Windbg命令脚本
命令脚本,就是将完成某个特定任务的相关命令组合在一起,保存在脚本文件里,加载到Windbg里执行,达到我们的目的.你可以理解为脚本就是一种语言,就像c或者汇编,但是他不需要编译器将其编译为可执行文件, ...
- 特征缩放(Feature Scaling)
特征缩放的几种方法: (1)最大最小值归一化(min-max normalization):将数值范围缩放到 [0, 1] 区间里 (2)均值归一化(mean normalization):将数值范围 ...
- 手动停止jquery ajax请求
主要调用jquery提供的ajax abort方法,详细代码如下: <html> <head> <meta charset="UTF-8"> & ...
- go的接口内部实现
1 前言 1.1 Go汇编 Go语言被定义为一门系统编程语言,与C语言一样通过编译器生成可直接运行的二进制文件.这一点与Java,PHP,Python等编程语言存在很大的不同,这些语言都是运行在基于C ...
- js获取url中的参数,并保证获取到的参数不乱码
//网上比较经典的js获取url中的参数的方法 function getQueryString(name) { var reg = new RegExp("(^|&)" + ...
- ES6 展开运算符 三个点实际功能
1.数组中使用let defaultColors = ['red', 'greed'] let favoriteColors = ['orange', 'yellow'] let fallColors ...
- 2019牛客国庆集训派对day1
C 存每个值存在的位置,枚举末尾的值,再枚举前面的值,哈希二分出最长相同的,即剩下的为不同的 D \(f_{i,j,k}\)为前i位,最后一个3因子在j,次因子在k G bitset处理有多少位置符合 ...