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 ...
随机推荐
- 解决jquery调用NET webservice跨域的问题
声明,解决方案由网上收集而来,个人整理.有别人的,也有我的. 一.webserive端 1.web.config 需要在web.config的configuration节点中加入如下的黑体部分内容. ...
- 实现ViewPager的联动效果
参考链接:android - Synchronizing two ViewPagers using OnPageChangeListener - Stack Overflow 其中有个非常完美的解决方 ...
- seienium基础(测试脚本中的等待方法)
测试脚本中的等待方法 一.加等待时间的目的 等待是为了使脚本执行更加稳定 二.常用的休眠方式 第一种 sleep(): 设置固定休眠时间.python 的 time 包提供了休眠方法 sleep() ...
- LaTeX的安装
1 下载与安装 下载地址. 选择清华TUNA开源镜像, 选择Full版本, 点击下载,按照提示安装,没有坑,就没有截图. 2 简单使用流程 1) 首先下载对应的LaTeX模板(从会议或者期刊网站上下载 ...
- 16-3-es5解析顺序
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- php 例子
图片上传 uploadify(flash版是免费的) 12个最好的 HTML5 jQuery 文件上传脚本 20款最好的jQuery文件上传插件
- https://webpack.js.org/plugins/
有问题还是看源码 ,看官方文档吧,整一晚上终于整明白了
- 【BZOJ4407】于神之怒加强版
题面 题目分析 \[ \begin{split} \sum\limits_{i=1}^n\sum\limits_{j=1}^mgcd(i,j)^k&=\sum\limits_{d=1}^nd^ ...
- Nginx反向代理Odoo并转为https
生成证书文件 生成自签名证书,并放在指定位置 $ openssl req -x509 -days 3650 -subj '/CN=odoo.youyun.com/' -nodes -newkey rs ...
- WildFly配置gzip压缩
使用jboss-cli.sh 执行下面的脚本 /subsystem=undertow/configuration=filter/gzip=gzipFilter:add() /subsystem=und ...