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. Django 第一天 开端

    今日内容: 一.HTTP协议 1.HTTP协议简介 参考博客:https://www.cnblogs.com/clschao/articles/9230431.html 是超文本传输协议 现在使用最广 ...

  2. learning java AWT 布局管理器CardLayout

    import javax.swing.*; import java.awt.*; import java.awt.event.ActionListener; public class CardLayo ...

  3. Centos 7 安装 dotnet 环境

    Centos 7 安装  dotnet 环境 下载官方 rpm yum 源 直接 yum install 安装rpm -Uvh https://packages.microsoft.com/confi ...

  4. php 简单使用redis 队列示例

    public function redisAction(){ $redis = new Redis(); $redis->connect('127.0.0.1', 6379); echo &qu ...

  5. 前端项目, 每次运行都需要输入 sudo 的解决方法

    前端项目, 每次运行都需要输入 sudo 的解决方法 node一直提示的sudo问题根本原因为: node 的所有者, 项目的所有者, 不同; 解决方法为: 将项目的所有者更改为 chown -R ` ...

  6. BurpSuite经常拦截firefox报文如success.txt的解决办法

    因为工作需要经常使用Burp对收发报文进行检测,平时习惯使用火狐浏览器,但是火狐浏览器经常进行一些登录状态的检测,导致Burp拦截中出现大量的火狐报文,如http://detectportal.fir ...

  7. 几款抓包工具在windows,mac,linux下的支持分析

    抓包工具的使用 几款抓包工具在windows,mac,linux下的支持分析 抓包工具简介 Chrome/Firefox 开发者工具: 浏览器内置,方便易用 Fiddler/Charles: 基于代理 ...

  8. goland 安装破解

    链接:https://pan.baidu.com/s/1vH70CHq122RbfwLwbHewjg  密码:zilv 复制如下注册码: 56ZS5PQ1RF-eyJsaWNlbnNlSWQiOiI1 ...

  9. 开启和关闭oracle数据库中的审计功能

    第1步:查看审计功能是否开启?SQL> show parameter audit;NAME                                 TYPE        VALUE-- ...

  10. mysql查看数据库表数量

    1.查看数据库表数量SELECT count(TABLE_NAME) FROM information_schema.TABLES WHERE TABLE_SCHEMA='dbname'; selec ...