Leetcode401Binary Watch二进制手表
二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59)。
每个 LED 代表一个 0 或 1,最低位在右侧。
给定一个非负整数 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 {
public:
vector<string> res;
int visit[10];
int visit2[60][60];
int times[10];//从小到大
vector<string> readBinaryWatch(int num)
{
if(num == 0)
{
res.push_back("0:00");
return res;
}
int x = 1;
for(int i = 0; i < 6; i++)
{
times[i] = x;
x *= 2;
}
x = 1;
for(int i = 6; i < 10; i++)
{
times[i] = x;
x *= 2;
}
DFS(num, 0, 0);
return res;
}
void DFS(int n, int h, int m)
{
if(n == 0)
{
if(visit2[h][m] == 1)
return;
string str = "";
if(h == 0)
{
str = str + '0';
}
else
{
str = str + to_string(h);
}
str += ':';
if(m == 0)
{
str = str + "00";
}
else if(m < 10)
{
str = str + '0' + to_string(m);
}
else
{
str = str + to_string(m);
}
res.push_back(str);
visit2[h][m] = 1;
return;
}
for(int i = 0; i < 10; i++)
{
if(visit[i] == 1)
continue;
if(i < 6)
{
if(m + times[i] > 59)
continue;
visit[i] = 1;
DFS(n - 1, h, m + times[i]);
visit[i] = 0;
}
if(i >= 6)
{
if(h + times[i] > 11)
continue;
visit[i] = 1;
DFS(n - 1, h + times[i], m);
visit[i] = 0;
}
}
}
};
Leetcode401Binary 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 简单】 第九十三题 二进制手表
二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右侧. 例如,上面的二进制手表读取 “3:25”. ...
- 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,最低位在右侧. 例如,上面的二进制手表 ...
- 401 Binary Watch 二进制手表
详见:https://leetcode.com/problems/binary-watch/description/ C++: class Solution { public: vector<s ...
- LeetCode--401--二进制手表
问题描述: 二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右侧. 例如,上面的二进制手表读取 “3 ...
- leetcode-二进制手表
二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右侧. 例如,上面的二进制手表读取 “3:25”. ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- LeetCode算法题-Binary Watch(Java实现)
这是悦乐书的第216次更新,第229篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第84题(顺位题号是401).二进制手表顶部有4个LED,代表小时(0-11),底部的6 ...
随机推荐
- 处理提交html危险代码的异常方法
当向asp.net mvc提交有html标签的值时,服务器会报检测到潜在危险的Request.Form值,处理方法如下: 1:在接收处理的Action方法上面加上 [VaildateInput(fal ...
- wangEditor富文本框——例
官方文档:http://www.wangeditor.com/ 效果 html <!DOCTYPE html> <html> <head> <meta cha ...
- linux 系统优化初始化配置
一.系统优化配置 1.修改yum源 配置国内yum源 阿里云yum源地址 #CentOS 5.x wget -O /etc/yum.repos.d/CentOS-Base.repo http://m ...
- 用shell编写小九九乘法表程序
1.使用for循环 运行结果: 2.方法二:for循环 运行结果: 备注: 1. echo -n 的意思是不自动换行,因为在linux shell中 echo到最后一个字符时会自动换行的,所以echo ...
- ThinkPHP可以支持直接使用字符串作为查询条件
ThinkPHP可以支持直接使用字符串作为查询条件,但是大多数情况推荐使用数组或者对象来作为查询条件,因为会更加安全. 大理石平台哪家好 一.使用字符串作为查询条件 这是最传统的方式,但是安全性不高, ...
- 「BZOJ2300」[HAOI2011] 防线修建
传送门 操作离线之后倒着做,只有加点操作. 用set动态维护凸包即可. //Achen #include<algorithm> #include<iostream> #incl ...
- System.Web.Mvc.FileResultc.sc
ylbtech-System.Web.Mvc.FileResultc.sc 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, Public ...
- PAT甲级——A1095 Cars on Campus
Zhejiang University has 8 campuses and a lot of gates. From each gate we can collect the in/out time ...
- 使用jquery的lazy loader插件实现图片的延迟加载
当网站上有大量图片要展示的话,如果一次把所有的图片都加载出来的话,这势必会影响网站的加载速度,给用户带来比较差的体验.通过使用jquery的lazy loader插件可以实现图片的延迟加载,当网页比较 ...
- 删除文件夹时提示“You need permission to perform this action。。。”,如何解决?
Win10系统,有时,要删除某个文件夹,却提示“You need permission to perform this action...” 以下是我Google之后找到的解决方案 1.创建一个文本文 ...