Backtracking-401. Binary Watch
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59).
Each LED represents a zero or one, with the least significant bit on the right.
For example, the above binary watch reads "3:25".
Given a non-negative integer n which represents the number of LEDs that are currently on, return all possible times the watch could represent.
Example:
Input: n = 1
Return: ["1:00", "2:00", "4:00", "8:00", "0:01", "0:02", "0:04", "0:08", "0:16", "0:32"]
Note:
- The order of output does not matter.
- The hour must not contain a leading zero, for example "01:00" is not valid, it should be "1:00".
- The minute must be consist of two digits and may contain a leading zero, for example "10:2" is not valid, it should be "10:02".
class Solution {
public:
void DFS(int len, int k, int curIndex, int val, vector<int>& vec)
{
if(k== && len== && val < ) vec.push_back(val);
if(k== && len== && val < ) vec.push_back(val);
if(curIndex == len || k == ) return;
DFS(len, k, curIndex+, val, vec);
val += pow(, curIndex), k--, curIndex++;
DFS(len, k, curIndex, val, vec);
} vector<string> readBinaryWatch(int num) {
vector<string> ans;
for(int i = max(, num-); i <= min(, num); i++)
{
vector<int> vec1, vec2;
DFS(, i, , , vec1), DFS(, num-i, , , vec2);
for(auto val1: vec1)
for(auto val2: vec2)
{
string str = (to_string(val2).size()==?"":"") + to_string(val2);
ans.push_back(to_string(val1)+":"+ str);
}
}
return ans;
}
};
Backtracking-401. Binary Watch的更多相关文章
- 401. Binary Watch
[题目] Total Accepted: 10330 Total Submissions: 24061 Difficulty: Easy Contributors: Admin A binary wa ...
- 27. leetcode 401. 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&Python] Problem 401. Binary Watch
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom ...
- 401. 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. 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. Binary Watch 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 java解法 Python解法 日期 [LeetCo ...
- [leetcode] 401. Binary Watch
https://leetcode.com/contest/5/problems/binary-watch/ 这个题应该是这次最水的,这个题目就是二进制,然后是10位,最大1024,然后遍历,找二进制里 ...
- 401 Binary Watch 二进制手表
详见:https://leetcode.com/problems/binary-watch/description/ C++: class Solution { public: vector<s ...
- LeetCode_401. Binary Watch
401. Binary Watch Easy A binary watch has 4 LEDs on the top which represent the hours (0-11), and th ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
随机推荐
- YAML教程
一.简介 YAML是一种人们可以轻松阅读的数据序列化格式,并且它非常适合对动态编程语言中使用的数据类型进行编码.YAML是YAML Ain't Markup Language简写,和GNU(" ...
- 6. Manage the driver for browser and the script for Hub
- 3、基本的Get和Post访问(含代理请求)
Get方式访问 HttpClient hc = new DefaultHttpClient(); HttpUriRequest request = new HttpGet("http://w ...
- 2018.10.05 NOIP模拟 相遇(dfs序+lca)
传送门 考虑到两条路径相交的条件: 设两条路径为a,ba,ba,b. 则要么aaa路径的lcalcalca在bbb上. 要么bbb路径的lcalcalca在aaa上. 因此我们维护两棵树. 分别支持路 ...
- C++之类和对象的使用(二)
析构函数 析构函数的作用并不是删除对象,而是在撤销对象占用的内存之前完成一系列清理工作,使这部分内存可以被程序分配给新对象使用.对象生命周期结束,程序就自动执行析构函数来完成这些工作. 析构函数是一种 ...
- Navicat如何导出数据库的svg、pdf,png图片
有时候各位可能有这么一种感觉,如果一个数据库中的表太多的话,查看起来不大方便,如果你习惯用navicat软件来查看er图的话,那也是更困难了,这里介绍一种方法,就是把这些关系结构导出一个可以用浏览器打 ...
- python 按行读取判断是否为空
for line in fr.readlines(): try: # print(len(line)) if(len(line)==1): continue else: fw.write(matche ...
- Oracle之SQL语句性能优化(34条优化方法)
(1)选择最有效率的表名顺序(只在基于规则的优化器中有效): ORACLE的解析器按照从右到左的顺序处理FROM子句中的表名,FROM子句中写在最后的表(基础表 driving table)将被最先处 ...
- day11(多线程,唤醒机制,生产消费者模式,多线程的生命周期)
A:进程: 进程指正在运行的程序.确切的来说,当一个程序进入内存运行,即变成一个进程,进程是处于运行过程中的程序,并且具有一定独立功能. B:线程: 线程是进程中的一个执行单元,负责当前进程中程序的执 ...
- (并查集)Travel -- hdu -- 5441(2015 ACM/ICPC Asia Regional Changchun Online )
http://acm.hdu.edu.cn/showproblem.php?pid=5441 Travel Time Limit: 1500/1000 MS (Java/Others) Memo ...