401 Binary Watch 二进制手表
详见:https://leetcode.com/problems/binary-watch/description/
C++:
class Solution {
public:
vector<string> readBinaryWatch(int num)
{
vector<string> res;
vector<int> hour{8, 4, 2, 1}, minute{32, 16, 8, 4, 2, 1};
for (int i = 0; i <= num; ++i)
{
vector<int> hours = generate(hour, i);
vector<int> minutes = generate(minute, num - i);
for (int h : hours)
{
if (h > 11)
{
continue;
}
for (int m : minutes)
{
if (m > 59)
{
continue;
}
res.push_back(to_string(h) + (m < 10 ? ":0" : ":") + to_string(m));
}
}
}
return res;
}
vector<int> generate(vector<int>& nums, int cnt)
{
vector<int> res;
helper(nums, cnt, 0, 0, res);
return res;
}
void helper(vector<int>& nums, int cnt, int pos, int out, vector<int>& res)
{
if (cnt == 0)
{
res.push_back(out);
return;
}
for (int i = pos; i < nums.size(); ++i)
{
helper(nums, cnt - 1, i + 1, out + nums[i], res);
}
}
};
参考:https://www.cnblogs.com/grandyang/p/5896454.html
401 Binary Watch 二进制手表的更多相关文章
- [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:二进制手表【401】
LeetCode:二进制手表[401] 题目描述 二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右 ...
- Java实现 LeetCode 401 二进制手表
401. 二进制手表 二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右侧. 例如,上面的二进制手表 ...
- 【LeetCode】401. Binary Watch 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 java解法 Python解法 日期 [LeetCo ...
- 【leetcode 简单】 第九十三题 二进制手表
二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右侧. 例如,上面的二进制手表读取 “3:25”. ...
- 【LeetCode-面试算法经典-Java实现】【067-Add Binary(二进制加法)】
[067-Add Binary(二进制加法)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given two binary strings, return thei ...
- Leetcode401Binary Watch二进制手表
二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右侧. 给定一个非负整数 n 代表当前 LED 亮着 ...
- [LeetCode] Prime Number of Set Bits in Binary Representation 二进制表示中的非零位个数为质数
Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...
- [LeetCode] Binary Gap 二进制间隙
Given a positive integer N, find and return the longest distance between two consecutive 1's in the ...
随机推荐
- Linux下汇编语言学习笔记11 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- 创建Django项目(七)——表单
2013-08-15 19:43:01| 1.URL配置和视图 "blog\urls.py"文件中:添加url(r'write_article/$', 'write ...
- APPLE STORE
直接在设置中,使用查看APPLE ID是无法更改的,现在必须要有所在区域的信用卡信息,支付方式无法像以前一样选择“无”. 查询后发现,有人说icloud3.0,即这个旧版的可以进行更改,于是下载. 但 ...
- AIM Tech R3 div2 E Centroid
思路很明显了,假设是点x,则看它的子树中是否有大于n/2的,如果有,则在该子树中剪去它可以剪的且小于n/2的,接到点x上. 则统计出在以x点为根的子树中,它的各子树可以剪去的且小于n/2的最大子子树. ...
- 【Python】python扩展
当python的基本功能无法满足要求.或者是为了保密源码(.py).遇到性能瓶颈时,我们经常要扩展python,扩展语言能够是C/C++.Java.C#等. 为python创建扩展须要三个基本的步骤: ...
- 何时、怎样开启 MySql 日志?
假如你是一名 web 开发者.假设你想调试你的应用或提升其性能的话,那你须要去參考各种日志文件.日志是開始故障排除最好的选择.就著名的 MySql 数据库server而言,你须要參考下面日志文件: 错 ...
- 连接App.config
ConfigurationManager.AppSettings["AdminName"]; 连接App.config的字符
- 使用heartbeat+monit实现主备双热备份系统
一.使用背景 项目须要实现主备双热自己主动切换的功能,保证系统7*24小时不间断执行.现已有两台双网卡的IBM的server,为了不再添加成本採购独立外部存储设备和双机热备软件.採用了linux下开源 ...
- 在java程序中,对于数据的输入/输出操作以“流”(stream)方式进行
在java程序中,对于数据的输入/输出操作以“流”(stream)方式进行
- SpringMVC_1
//@SessionAttributes(value={"user"},types={String.class}) @Controller public class SpringM ...