401 Binary Watch 二进制手表
详见:https://leetcode.com/problems/binary-watch/description/
C++:
class Solution {
public:
vector<string> readBinaryWatch(int num)
{
vector<string> res;
vector<int> hour{8, 4, 2, 1}, minute{32, 16, 8, 4, 2, 1};
for (int i = 0; i <= num; ++i)
{
vector<int> hours = generate(hour, i);
vector<int> minutes = generate(minute, num - i);
for (int h : hours)
{
if (h > 11)
{
continue;
}
for (int m : minutes)
{
if (m > 59)
{
continue;
}
res.push_back(to_string(h) + (m < 10 ? ":0" : ":") + to_string(m));
}
}
}
return res;
}
vector<int> generate(vector<int>& nums, int cnt)
{
vector<int> res;
helper(nums, cnt, 0, 0, res);
return res;
}
void helper(vector<int>& nums, int cnt, int pos, int out, vector<int>& res)
{
if (cnt == 0)
{
res.push_back(out);
return;
}
for (int i = pos; i < nums.size(); ++i)
{
helper(nums, cnt - 1, i + 1, out + nums[i], res);
}
}
};
参考:https://www.cnblogs.com/grandyang/p/5896454.html
401 Binary 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:二进制手表【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,最低位在右侧. 例如,上面的二进制手表 ...
- 【LeetCode】401. Binary Watch 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 java解法 Python解法 日期 [LeetCo ...
- 【leetcode 简单】 第九十三题 二进制手表
二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右侧. 例如,上面的二进制手表读取 “3:25”. ...
- 【LeetCode-面试算法经典-Java实现】【067-Add Binary(二进制加法)】
[067-Add Binary(二进制加法)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given two binary strings, return thei ...
- Leetcode401Binary Watch二进制手表
二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右侧. 给定一个非负整数 n 代表当前 LED 亮着 ...
- [LeetCode] Prime Number of Set Bits in Binary Representation 二进制表示中的非零位个数为质数
Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...
- [LeetCode] Binary Gap 二进制间隙
Given a positive integer N, find and return the longest distance between two consecutive 1's in the ...
随机推荐
- Delphi:校验手机号及身份证号
//校验手机号 function IsMobileNumber( num:string ):boolean; begin Result:=False; if length( tr ...
- python 交互模式 方向键乱码问题解决
python交互模式下通常用向上键来找到之前执行的命令,用左右键移动光标.这很方便.但有的时候这些键在按完后却会出现乱码. 本文只解决CentOS 6.4 下 python2.7.8 的乱码问题. 这 ...
- openFileOutput 文件属性设置、主动配置文件的可读写属性及事实上现方式
首先參考 Android 内部存储相关的函数(getCacheDir,getDir, getFileStreamPath,getFilesDir,openFileInput, ...) 1. 用ope ...
- 具体解释Android定位
相信非常多的朋友都有在APP中实现定位的需求,今天我就再次超炒冷饭,为大家献上国内开发人员经常使用到的三种定位方式.它们分别为GPS,百度和高德,惯例先简介下定位的背景知识. 什么是GPS定位.基站定 ...
- MAVEN项目模块化
maven的最大的特点之中的一个就是能够把项目模块化. 前面的一篇文章MAVEN创建并打包web项目已经创建了一个简单的webapp,注意这个webapp的打包方式是war. 假设如今又要划分出来一个 ...
- OpenLayers 3+Geoserver+PostGIS实现点击查询
WebGIS开发中,点击查询是最经常使用的一种查询方式,在ArcGIS api 中.这样的查询叫IdentifyTask,主要作用是前台提交參数.交ArcServer查询分析返回. 本文从开源框架的角 ...
- .Net 与 Javascript 混合编程系列
之前的文章有提到 edge 和 nodejs 交互,通过node的模块为C# 提供一些扩展,这个还是比較方便. 这里说下为什么要使用js. 1.SharpKit是一个用于在编译时将C#语言转换为Jav ...
- mysql查看所有存储过程,函数,视图,触发器,表
查询数据库中的存储过程和函数 方法一: select `name` from mysql.proc where db = 'your_db_name' and `type` = 'PROCEDURE' ...
- VS2012 ASP.NET 母版页的创建与使用
在做牛腩新闻公布系统的过程中,须要使用ASP.NET的母版页来抽出全部网页的公共部分,以便更好的复用自己的网页布局和设计. 首先我们来看怎样创建一个新的母版页,例如以下图所看到的: 加入之后,例如以下 ...
- 向海量用户发送数据哪家最强?上QDN下载LTE Broadcast SDK!
情境一: 在一个数万人的体育场内.作为一名观众你非常难看清运动员的面容.假设有了4G手机.你能够非常easy的打开直播应用.一边看直播讲解,一边体验现场气氛.但令人尴尬的是,现场几万人同一时候须要观看 ...