401. Binary Watch

Easy

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".
package leetcode.easy;

public class BinaryWatch {
public java.util.List<String> readBinaryWatch(int num) {
java.util.List<String> ret = new java.util.ArrayList<>(1024); for (int hour = 0; hour < 12; ++hour) {
for (int min = 0; min < 60; ++min) {
if (Integer.bitCount(hour) + Integer.bitCount(min) == num) {
ret.add(String.format("%d:%02d", hour, min));
}
}
} return ret;
} @org.junit.Test
public void test() {
System.out.println(readBinaryWatch(1));
}
}

LeetCode_401. Binary Watch的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id

    出现场景:当点击"分类"再返回"首页"时,发生error退出   BUG描述:Caused by: java.lang.IllegalArgumentExcep ...

  3. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  4. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  5. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  6. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

  7. Leetcode, construct binary tree from inorder and post order traversal

    Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...

  8. [LeetCode] Binary Watch 二进制表

    A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom ...

  9. [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

随机推荐

  1. [CodeForces - 906D] Power Tower——扩展欧拉定理

    题意 给你 $n$ 个 $w_i$ 和一个数 $p$,$q$个询问,每次询问一个区间 $[l,r] $,求 $w_l ^{w_{l+1}^{{\vdots}^{w_r}}} \ \% p$ 分析 由扩 ...

  2. python - alipay sdk 使用 及 注意点

    一. 在 点击跳转 这里拿到自己的 appid  和  支付宝公钥 ,    如果想要得到 支付宝的公钥 就需要获取 应用的公钥,具体获取方式 : 获取应用公钥和私钥 a. 应用私钥和支付宝公钥 获取 ...

  3. Node.js创建服务及实现静态资源托管/接口请求

    1.环境 采用12.13.x版本 2.创建server.js 文件内容如下: let http = require("http"); let fs = require(" ...

  4. LOJ P10176 最大连续和 题解

    每日一题 day29 打卡 Analysis 朴素的DP方程为: dp[i]=max{sum[i]-sum[j-1]}; 对于每个i 需要用单调队列维护最小的sum[j-1] 注意: 1.tail初值 ...

  5. linux 登录后有时候会出现-bash-4.1$

    转载自https://blog.csdn.net/jiedao_liyk/article/details/78470498 linux登录后有时候会出现-bash-4.1$ 造成这样的原因: 与这个用 ...

  6. 【Redis】Linux下Redis的安装

    Redis服务安装 主要有两种方式:apt安装和编译安装. 我采用的是apt安装,系统是ubuntu18.04,Redis version 5:4.0.9-1 在 Ubuntu 系统安装 Redi 可 ...

  7. BZOJ 1406: [AHOI2007]密码箱

    二次联通门 : BZOJ 1406: [AHOI2007]密码箱 /* BZOJ 1406: [AHOI2007]密码箱 数论 要求 x^2 ≡ 1 (mod n) 可以转换为 x ^ 2 - k * ...

  8. Sudoku(简单DFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5547 数据比较少,直接暴力DFS,检验成立情况即可 AC代码:但是不知道为什么用scanf,print ...

  9. linux如何实现凌晨定时执行脚本?

    Linux中,周期执行的任务一般由cron这个守护进程来处理.cron读取一个或多个配置文件,这些配置文件中包含了命令行及其调用时间.cron的配置文件称为“crontab”,是“cron table ...

  10. [luogu 5024] 保卫王国

    Problem Here Solution 这大概是一篇重复累赘的blog吧. 最小权覆盖集=全集-最大权独立集 强制取或不取,可以通过将权值修改成inf或者-inf 然后就用动态dp的套路就行了 动 ...