LeetCode--401--二进制手表
问题描述:
二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59)。
每个 LED 代表一个 0 或 1,最低位在右侧。
例如,上面的二进制手表读取 “3:25”。
给定一个非负整数 n 代表当前 LED 亮着的数量,返回所有可能的时间。
案例:
输入: n = 1
返回: ["1:00", "2:00", "4:00", "8:00", "0:01", "0:02", "0:04", "0:08", "0:16", "0:32"]
注意事项:
- 输出的顺序没有要求。
- 小时不会以零开头,比如 “01:00” 是不允许的,应为 “1:00”。
- 分钟必须由两位数组成,可能会以零开头,比如 “10:2” 是无效的,应为 “10:02”。
方法:
class Solution(object):
def readBinaryWatch(self, num):
"""
:type num: int
:rtype: List[str]
"""
res = []
for hour in range(12):
for minute in range(60):
if (bin(hour) + bin(minute)).count('') == num:
res.extend(["%d:%02d"%(hour,minute)])
return res
官方:
class Solution(object):
def readBinaryWatch(self, num):
"""
:type num: int
:rtype: List[str]
"""
a={0: [''], 1: ['', '', '', ''], 2: ['', '', '', '', ''], 3: ['', '']}
b={0: [''], 1: ['', '', '', '', '', ''], 2: ['', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], 3: ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], 4: ['', '', '', '', '', '', '', '', '', '', '', '', '', ''], 5: ['', '', '','']}
if num<0 or num>8:
return []
mi=max(0,num-5)
mx=min(3,num)
res=[]
for i in range(mi,mx+1):
for x in a[i]:
for y in b[num-i]:
res.append(x+':'+y)
return res
LeetCode--401--二进制手表的更多相关文章
- Java实现 LeetCode 401 二进制手表
401. 二进制手表 二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右侧. 例如,上面的二进制手表 ...
- LeetCode:二进制手表【401】
LeetCode:二进制手表[401] 题目描述 二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右 ...
- 【leetcode 简单】 第九十三题 二进制手表
二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右侧. 例如,上面的二进制手表读取 “3:25”. ...
- [Swift]LeetCode401. 二进制手表 | 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:二进制求和【67】
LeetCode:二进制求和[67] 题目描述 给定两个二进制字符串,返回他们的和(用二进制表示). 输入为非空字符串且只包含数字 1 和 0. 示例 1: 输入: a = "11" ...
- Leetcode401Binary Watch二进制手表
二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右侧. 给定一个非负整数 n 代表当前 LED 亮着 ...
- 401 Binary Watch 二进制手表
详见:https://leetcode.com/problems/binary-watch/description/ C++: class Solution { public: vector<s ...
- [leetcode] 401. Binary Watch
https://leetcode.com/contest/5/problems/binary-watch/ 这个题应该是这次最水的,这个题目就是二进制,然后是10位,最大1024,然后遍历,找二进制里 ...
- LeetCode 1290. 二进制链表转整数
地址 https://www.acwing.com/solution/LeetCode/content/7132/ 题目描述给你一个单链表的引用结点 head.链表中每个结点的值不是 0 就是 1.已 ...
- Leetcode 868. 二进制间距
868. 二进制间距 显示英文描述 我的提交返回竞赛 用户通过次数201 用户尝试次数220 通过次数207 提交次数396 题目难度Easy 给定一个正整数 N,找到并返回 N 的二进制表示中 ...
随机推荐
- python --- 20 约束 异常处理 MD5 日志
一.类的约束 1.抛出异常 NotImplementedError 2.抽象方法 含有抽象方法的类是抽象类 抽象类中的方法全是抽象方法的是接口 抽象类不能创建对象 二.异常处理 处理完后代码可继 ...
- ODAC(V9.5.15) 学习笔记(四)TOraDataSet
名称 类型 说明 SequenceMode TSequenceMode ODAC可以直接利用Oracle中的序列对象为表的主键赋值,从而实现主键自动增长的功能.该属性决定了在什么场合下使用序列: sm ...
- Python3 tkinter基础 LabelFrame Radiobutton 形成两组不相互限制的单选按钮
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Restful framework【第十二篇】版本控制
简单使用 -drf版本控制 -在setting中配置 'DEFAULT_VERSION': 'v1', # 默认版本(从request对象里取不到,显示的默认值) 'ALLOWED_VERSIONS' ...
- Concepts-->Migrations
https://flywaydb.org/documentation/migrations Overview With Flyway all changes to the database are c ...
- Could not stop Cortex-M device! please check the JTAG cable的解决办法
今天程序烧录后,进行调试时keil提示:Could not stop Cortex-M device! please check the JTAG cable 如图: 于是我在网上搜了一下, ...
- C#winform的textbox怎么设置滚动条
用 C#开发软件的时候文本框textbox是没有滚动条的,而且是单行文本.下面教大家如何设置多行,并且设置横向滚动条和垂直滚动条. 打开VS工具,创建一个winform窗体项目.系统会自动创建一个主窗 ...
- NOI 2011 阿狸的打字机(AC自动机+主席树)
题意 https://loj.ac/problem/2444 思路 多串匹配,考虑 \(\text{AC}\) 自动机.模拟打字的过程,先建出一棵 \(\text{Trie}\) 树,把它变成自动机 ...
- 几个C++ online test 网站
http://www.mycppquiz.com/list.php http://www.codelect.net/TestDetails/Cplusplus-Senior-Level-Test ht ...
- linux mysql操作命令大全
1.linux下启动mysql的命令:mysqladmin start/ect/init.d/mysql start (前面为mysql的安装路径) 2.linux下重启mysql的命令:mysqla ...