leetcode-二进制手表
二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59)。
每个 LED 代表一个 0 或 1,最低位在右侧。
例如,上面的二进制手表读取 “3:25”。
给定一个非负整数 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”。
思路: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;
}
};
bitCount
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-二进制手表的更多相关文章
- 【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,最低位在右侧. 例如,上面的二进制手表 ...
- [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 ...
- Leetcode401Binary Watch二进制手表
二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右侧. 给定一个非负整数 n 代表当前 LED 亮着 ...
- leetcode -- 二进制
leetcode -- 二进制 在学习编程语言的运算符时,大部分语言都会有与,或等二进制运算符,我在初期学习这些运算符的时候,并没有重点留意这些运算符,并且在后续的业务代码中也没有频繁的使用过,直到后 ...
- 401 Binary Watch 二进制手表
详见:https://leetcode.com/problems/binary-watch/description/ C++: class Solution { public: vector<s ...
- LeetCode 二进制问题
338. Counting Bits(计算小于n的各个数值对应的二进制1的个数) 思路:通过奇偶判断,if i是偶数,a[i]=a[i/2],if i是奇数,a[i]=a[i-1]+1. class ...
- leetcode 二进制求和 python
class Solution: def addBinary(self, a, b): """ :type a: str :type b: str :rtype: str ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- 第23章 I2C—读写EEPR
本章参考资料:<STM32F76xxx参考手册>.<STM32F7xx规格书>.库帮助文档<STM32F779xx_User_Manual.chm>及<I2C ...
- Vue01 vue基础、mvvm、ES6z知识点、计算属性、生命周期
Vue案例: <body> <div id="app"> <!--第一部分--> <fieldset> <legend> ...
- 小程序内嵌H5——判断小程序环境的坑
现在各种小程序风靡,这边H5的需求还没有搞定,产品又要求做小程序版本,做可以,关键是618前上线,我-- whatever,618要做推广,日期订了,剩下的就只能是排期,定方案,尽可能完成. 最后和产 ...
- JS-类型相关
typeof检测类型typeof 返回的数据类型种类:number(js不分整形,浮点等等 所有的数字都是number类型).string.boolean.undefined.object.funct ...
- 使用什么进行app开发
HTML5+plus, Hbuilder HTML5+plus介绍 HTML5 Plus移动App,简称5+App,是一种基于HTML.JS.CSS编写的运行于手机端的App,这种App可以通过扩展的 ...
- consonant_摩擦音_咬舌音
consonant_摩擦音_咬舌音_[θ]和[ð].[h] 咬舌音:咬住舌尖发音. [θ]:牙齿咬住舌尖,送气,气流摩擦发出声音,声带不振动: faith.thank.healthy.both.th ...
- pip快速git项目安装
pip install git+https://github.com/xx/xx.git
- HyperLedger Fabric 1.4 基础环境搭建(7)
学习了前面几章理论知识后,本章开始介绍实践操作,先介绍Fabric基础环境搭建,采用的操作系统为Centos 7 64位,依次介绍Docker安装.Docker-Compose安装.GO语言环境安装. ...
- Java——多线程---18.11.22
多线程代码:Runnable方法 package com.hebust.java.third; import java.util.Random; public class SaleTicket imp ...
- mybatis入门(一):jdbc的缺点
mybatis的基础内容 1.mybatis的框架原理 2.mybatis开发dao两种方法: a.原始dao开发方法(程序需要编写dao接口和dao实现类) b.mybatis的mapper接口(相 ...