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 ...
随机推荐
- vue项目实现按需加载的3种方式
vue异步组件技术 vue-router配置路由,使用vue的异步组件技术,可以实现按需加载.这种方式下一个组件生成一个js文件 用例: { path: '/promisedemo', name: ' ...
- 关于Slice的一些补充说明
s[m:n:l] 规则总结如下. (1) 范围 [m,n),从m开始轮询:超出范围后选边界值. l>0 l<0 关于边界值 (2) 把字符串完全反序,用 s[::-1]. 有些文档上说 s ...
- 平衡树模板【splay的实现】
[平衡树splay实现] 无注释代码 #include<bits/stdc++.h> using namespace std; typedef long long LL; ,MAXN=1e ...
- 设定计算属性setter
<!DOCTYPE html> <html lang="zh"> <head> <title></title> < ...
- 模板——二分图匹配KM
具体方法就不介绍了,详见 https://blog.csdn.net/sixdaycoder/article/details/47720471 主要讲一些注意点: 1:不直接将未匹配的y减小是因为要保 ...
- jquery移除元素某个属性
removeAttr() 方法从被选元素中移除属性. 例如:$("p").removeAttr("style");
- 构建HBase二级索引
- 将wordpress中的文章导出为markdown
一.进入wordpress后台,选择工具-导出数据,选择你需要导出的内容.文章等,会下载一个xml文件到本地电脑 二.使用一个名为wordpress-to-markdown的工具 源码地址:wordp ...
- python configparser模块详解
此模块提供了一个实现基本配置语言的类 首先来看一个非常基本的配置文件,如下所示格式: [DEFAULT] ServerAliveInterval = 45 Compression = yes Comp ...
- cf519E
传送门 多组询问,问到树上两个点x,y距离相等的点的个数. 倍增求lca. //Twenty #include<cstdio> #include<cstdlib> #inclu ...