LeetCode_401. Binary Watch
401. Binary Watch
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的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id
出现场景:当点击"分类"再返回"首页"时,发生error退出 BUG描述:Caused by: java.lang.IllegalArgumentExcep ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
- 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 ...
- [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 ...
- [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
随机推荐
- Oracle 新增数据 insert into整理
一.普遍的方法:insert into 表名(id,name,age,status,字段N) values('id','name','age','status','字段N'); --建议用这个 ...
- Java - Annotation使用
本文转载于(这个写的很好):https://www.cnblogs.com/be-forward-to-help-others/p/6846821.html Annotation Annotation ...
- if语句:若边长小于等于0,则不进行计算;否则,计算正方形的面积
#include<stdio.h>void main(){ float a; printf("Input the value:"); scanf("%f&qu ...
- learning java AWT 布局管理器 GridLayout
GridLayout布局管理器将容器分割成纵横线分格的网格,每个网格所占的区域大小相同. import java.awt.*; public class GridLayoutTest { public ...
- web基本概念
一.互联网 互联网(英语:Internet),又称网际网络,或音译因特网(Internet).英特网,互联网始于1969年美国的阿帕网.是网络与网络之间所串连成的庞大网络,这些网络以一组通用的协议相连 ...
- RookeyFrame Bug 编号显示 系统自动生成 的问题,有时候依旧会显示text文本框
编号显示 系统自动生成 的问题,有时候依旧会显示text文本框 1.在线新建model -> 启用编码规则 -> 新建字段Code(主键) 2.跟Code字段 创建编码规则 3.新增菜单 ...
- input file标签限制上传文件类型
用 input 的file类型标签上传文件,有时需要限制上传文件类型,添加accept属性可以实现 <input type="file" accept="image ...
- Linux 命令集合之进程查看命令-软件安装命令-文件查找命令-主机修改命令
1.dpkg 的使用 dpkg -s ssh 查看是否按装了ssh软件 dpkg -L ssh 查看ssh安装的关联目录结构和安装位置 dpkg -i vim.deb 本地离线安装vim编辑器 apt ...
- Pytest权威教程15-运行Nose用例
目录 运行Nose用例 使用方法 支持的nose风格 不支持的习语/已知问题 返回: Pytest权威教程 运行Nose用例 Pytest基本支持运行Nose框架格式的测试用例. 使用方法 后安装py ...
- hive(2)数据类型和文件格式
基本的数据类型 Hive支持关系型数据中大多数基本的数据类型,同时也支持关系型数据库中很少出现的三种集合数据类型. 集合数据类型 Hive中的列支持使用struct.map.array集合数据类型,下 ...