[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- ...
随机推荐
- poj1860 Currency Exchange(spfa判断是否存在正环)
题意:有m个货币交换点,每个点只能有两种货币的互相交换,且要给佣金,给定一开始的货币类型和货币数量,问若干次交换后能否让钱增加. 思路:spfa求最长路,判断是否存在正环,如果存在则钱可以在环中一直增 ...
- 《Maven在Java项目开发中的应用》论文笔记(十七)
标题:Maven在Java项目开发中的应用 一.基本信息 时间:2019 来源:山西农业大学 关键词:Maven:Java Web:仓库:开发人员:极限编程; 二.研究内容 1.Maven 基本原理概 ...
- django 进行语言的国际化及在后台进行中英文切换
项目的部署地为: 中国大陆与美国东海岸, 两个地区的服务器数据不进行同步, 中国地区的服务器页面展示中文, 美国地区的服务器页面展示成英文, 项目后台使用python编程语言进行开发, 并结合djan ...
- ICMP隧道
参考文章:http://www.sohu.com/a/297393423_783648
- 2、HDFS交互式Shell
管理模式 bin/hdfs dfsadmin ## run a hdfs admin client bin/hdfs dfsadmin -report ##报告信息 bin/hdfs dfsadmin ...
- Spring Security 认证执行流程
本文基于 Spring Security 5.x 推荐阅读: 项目集成Spring Security SpringSecurity 整合 JWT 一.外层-正常登陆调用 项目启动后会自动寻找 User ...
- PHP-FPM config 文件生产环境
;;;;;;;;;;;;;;;;;; ; Global Options ; ;;;;;;;;;;;;;;;;;; [global] pid = run/php-fpm.pid error_log = ...
- JavaScript高级程序编程(二)
JavaScript 基本概念 1.区分大小写,变量名test与Test 是两个不同的变量,且函数命名不能使用关键字/保留字, 变量命名规范: 开头字符必须是字母,下划线,或者美元符号,ECMAScr ...
- tensorflow学习笔记(二)
tensorflow中自带的mnist手写数字识别,运用最简单的单层神经网络,softmax激活函数,极客学院上说准确率有91%,我今天调整到了92%! import tensorflow as tf ...
- sublime 添加到右键菜单