二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59)。

每个 LED 代表一个 0 或 1,最低位在右侧。

例如,上面的二进制手表读取 “3:25”。

给定一个非负整数 代表当前 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”。

思路:1,2,4,8,16进行组合。 与组合总数类型的题目相似:leetcode-组合总数III(回溯)

但是数字为小时和分钟,因此通过一个k和num-k来控制小时和分钟的个数。

具体代码如下:

//思路: 通过1,2,4,8的组合获得h和m的可能性,与combination相似。每次组合的过程中,从小时中取出i个,从分钟取出num-i个。
//组合的思想为回溯: 与组合类似,取出多少个数字进行组合。
class Solution {
public:
void dfs(vector<int>& nums,vector<int>& temp,int count,int out,int start){
if(count==){
temp.push_back(out);
return;
}
for(int i=start;i<nums.size();i++){
dfs(nums,temp,count-,out+nums[i],i+);
}
}
vector<int> help(vector<int>& nums,int count){
vector<int> temp;
dfs(nums,temp,count,,);
return temp;
}
vector<string> readBinaryWatch(int num) {
vector<string> res;
vector<int> hours{,,,},mins{,,,,,};
for(int k=;k<=num;k++){
vector<int> hour=help(hours,k);
vector<int> min=help(mins,num-k);
for(int h:hour){
if(h>)continue;
for(int m:min){
if(m>)continue;
res.push_back(to_string(h)+(m>?":":":0")+to_string(m));
}
}
}
return res;
}
};
奇淫技巧!!!
Java  利用String 函数的format,可以将字符串特定格式化输出!!!

bitCount

public static int bitCount(int i)
返回指定 int 值的二进制补码表示形式的 1 位的数量。比如bitCount(1) =1;  (2)=1 ;  (3)=2;
class Solution {
public List<String> readBinaryWatch(int num) {
List<String> res=new ArrayList();
for(int h=0;h<12;h++){
for(int m=0;m<60;m++){
if(Integer.bitCount(h)+Integer.bitCount(m)==num)
res.add(String.format("%d:%02d",h,m));
}
}
return res;
}
}

leetcode-二进制手表的更多相关文章

  1. 【leetcode 简单】 第九十三题 二进制手表

    二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右侧. 例如,上面的二进制手表读取 “3:25”. ...

  2. LeetCode:二进制手表【401】

    LeetCode:二进制手表[401] 题目描述 二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右 ...

  3. Java实现 LeetCode 401 二进制手表

    401. 二进制手表 二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右侧. 例如,上面的二进制手表 ...

  4. [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 ...

  5. Leetcode401Binary Watch二进制手表

    二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右侧. 给定一个非负整数 n 代表当前 LED 亮着 ...

  6. leetcode -- 二进制

    leetcode -- 二进制 在学习编程语言的运算符时,大部分语言都会有与,或等二进制运算符,我在初期学习这些运算符的时候,并没有重点留意这些运算符,并且在后续的业务代码中也没有频繁的使用过,直到后 ...

  7. 401 Binary Watch 二进制手表

    详见:https://leetcode.com/problems/binary-watch/description/ C++: class Solution { public: vector<s ...

  8. LeetCode 二进制问题

    338. Counting Bits(计算小于n的各个数值对应的二进制1的个数) 思路:通过奇偶判断,if i是偶数,a[i]=a[i/2],if i是奇数,a[i]=a[i-1]+1. class ...

  9. leetcode 二进制求和 python

    class Solution: def addBinary(self, a, b): """ :type a: str :type b: str :rtype: str ...

  10. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

随机推荐

  1. phpstudy 出现You don't have permission to access / on this server.

    You don't have permission to access / on this server. 去掉vhost文件中的Options FollowSymLinks ExecCGI 第一次发 ...

  2. 在spring添加注解时,第一行package报错configure build path

    练习spring的ioc的注解的时候写上注解就会在第一行package报错configure build path. 用的spring4.2.4的jar包.经过上网查阅资料,可能是jar包冲突,解决办 ...

  3. php导出word格式数据的代码(转)

    本节内容:一个php导出文档的类 例子:<?php /*** 生成word文档的类* by www.jbxue.com*/class word{     function start()    ...

  4. redis存储数据的时候

    使用redis存储数据的时候,有时候为了查看的方便,通常会有层级或者说是目录, 这时候我们在set的时候,需要将key值使用“:”的符号来区分层级关系,比如:set(“a:b”, “123”),那么在 ...

  5. 转:system.Security.Cryptography C# 加密和解密

    以下文转自: http://www.360doc.com/content/13/0122/05/19147_261678471.shtml 总结:注册的时候经过MD5加密存进数据库,在登录的时候需要先 ...

  6. iOS视频倒放

    iOS视频倒放 视频的倒放就是视频从后往前播放,这个只适应于视频图像,对声音来说倒放只是噪音,没什么意义,所以倒放的时候声音都是去除的. 倒放实现 一般对H264编码的视频进行解码,都是从头至尾进行的 ...

  7. React--- react 初见React 总结

    简介 react 程序代码是透明的,需要什么装什么 代码实现逻辑清晰可见 第一天 React  基础构造 分别是  继承的 React.component(继承的依赖类)/dom(dom元素)/pro ...

  8. tomcat安装、配置相关的几个点

    Connector port="8080"HTTP协议的默认端口号:8080 FTP协议的默认端口号:21 1.tomcat的安装目录要与Java jre的安装目录一致. bin: ...

  9. Python-调用系统指令小记

    import subprocess def exec_command(cmd, log_path, **kwargs): with open(log_path, 'w') as f: p = subp ...

  10. Xshell配色方案推荐

    使用方法: 新建mycolor.xcs文件 复制粘贴如下代码,将文件导入,修改自己喜欢的字体即可 [mycolor] text=00ff80 cyan(bold)=00ffff text(bold)= ...