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. Linux安装版本solr-5.3.0

    准备材料:solr-5.3.0.tgz 下载路径:http://mirrors.hust.edu.cn/apache/lucene/solr/ 安装solr 1.解压solr [root@svn-se ...

  2. tensorflow 运行效率 GPU memory leak 问题解决

    问题描述: Tensorflow 训练时运行越来越慢,重启后又变好. 用的是Tensorflow-GPU 1.2版本,在GPU上跑,大概就是才开始训练的时候每个batch的时间很低,然后随着训练的推进 ...

  3. 【CSP-S 2019】【洛谷P5665】划分【单调队列dp】

    前言 \(csp\)时发现自己做过类似这道题的题目 : P4954 [USACO09Open] Tower of Hay 干草塔 然后回忆了差不多\(15min\)才想出来... 然后就敲了\(88p ...

  4. 洛谷 P2401 不等数列 题解

    每日一题 day25 打卡 Analysis dp[i][j]=dp[i-1][j-1]*(i-j)+dp[i-1][j]*(j+1); 其中i和j是表示前i个数中有j个小于号,j<=i-1 要 ...

  5. CF19D Points 平衡树

    题意:支持插入/删除点 $(x,y)$,查询一个点右上方横坐标与之最接近的点坐标. 我们可以对于每一个操作过的横坐标都开一个 $set$,然后再开一个平衡树,维护每个横坐标上最大的纵坐标. 然后查询点 ...

  6. (16)打鸡儿教你Vue.js

    博客: Hexo搭建个性博客 https://hexo.io/zh-cn/ 快速.简洁且高效的博客框架 超快速度 Node.js 所带来的超快生成速度,让上百个页面在几秒内瞬间完成渲染. 支持 Mar ...

  7. react 通过 xlink 方式引用 iconfont

    项目中采用 xlink 的方式引用 iconfont 文件,在正常的 html 文件中可以正常引用,但是在 react 下确不可以运行. 经过查找,发现需要更改如下 引入的属性默认为 xlink-hr ...

  8. 《挑战30天C++入门极限》新手入门:C/C++中的结构体

        新手入门:C/C++中的结构体 什么是结构体? 简单的来说,结构体就是一个可以包含不同数据类型的一个结构,它是一种可以自己定义的数据类型,它的特点和数组主要有两点不同,首先结构体可以在一个结构 ...

  9. vuex(用了vue就上了一条不归路的贼船)

    一.Vuex是干什么用的? 它是用于对复杂应用进行状态管理用的(官方说法是它是一种状态管理模式). “杀鸡不用宰牛刀”.对于简单的项目,根本用不着Vuex这把“宰牛刀”.那简单的项目用什么呢?用Vue ...

  10. 小程序在选择某一个东西的时候,可以用if,else 来做

    <view class='fake-select-item-text brand-selected' wx:if='{{selectedBrandName}}'> {{selectedBr ...