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".

Solution 1: Bit Manipulation

use   Integer.bitCount()

 public class Solution {
public List<String> readBinaryWatch(int num) {
List<String> res = new ArrayList<String>();
for (int i=0; i<12; i++) {
for (int j=0; j<60; j++) {
if (Integer.bitCount(i) + Integer.bitCount(j) == num) {
String str1 = Integer.toString(i);
String str2 = Integer.toString(j);
res.add(str1 + ":" + (j<10? "0"+str2 : str2));
}
}
}
return res;
}
}

Solution 2: Backtracking, 非常精妙之处在于用了两个数组来帮助generate digit(例如:1011 -> 11)

 public class Solution {
public List<String> readBinaryWatch(int num) {
int[] nums1 = new int[]{8, 4, 2, 1}, nums2 = new int[]{32, 16, 8, 4, 2, 1};
List<String> res = new ArrayList<String>();
for (int i=0; i<=num; i++) {
List<Integer> hours = getTime(nums1, i, 12);
List<Integer> minutes = getTime(nums2, num-i, 60);
for (int hour : hours) {
for (int minute : minutes) {
res.add(hour + ":" + (minute<10? "0"+minute : minute));
}
}
}
return res;
} public List<Integer> getTime(int[] nums, int count, int limit) {
List<Integer> res = new ArrayList<Integer>();
getTimeHelper(res, count, 0, 0, nums, limit);
return res;
} public void getTimeHelper(List<Integer> res, int count, int pos, int sum, int[] nums, int limit) {
if (count == 0) {
if (sum < limit)
res.add(sum);
return;
}
for (int i=pos; i<nums.length; i++) {
getTimeHelper(res, count-1, i+1, sum+nums[i], nums, limit);
}
}
}

Leetcode: Binary Watch的更多相关文章

  1. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

  2. LeetCode: Binary Tree Traversal

    LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...

  3. [LeetCode] Binary Search 二分搜索法

    Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...

  4. LeetCode Binary Search All In One

    LeetCode Binary Search All In One Binary Search 二分查找算法 https://leetcode-cn.com/problems/binary-searc ...

  5. LeetCode & Binary Search 解题模版

    LeetCode & Binary Search 解题模版 In computer science, binary search, also known as half-interval se ...

  6. [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 ...

  7. [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  8. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  9. [LeetCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  10. [LeetCode] Binary Tree Right Side View 二叉树的右侧视图

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

随机推荐

  1. 资源(1)----封装类(连接数据库mysql,分页)

    一,链接MYSQL数据库 class DBDA{ public $host="localhost";//服务器地址 public $uid="root";//数 ...

  2. Linguistic corpora 种子语料库-待分析对象-分析与更新语料库

    Computational Linguistics http://matplotlib.org/ https://github.com/matplotlib/matplotlib/blob/maste ...

  3. os

    内核,Shell和文件结构一起形成了基本的操作系统结构. from:大学生攻克Linux系统教程(又名天下没有难学的Linux) 发问: 0-内核,再怎么分出层次呢?

  4. 4Web镇之旅:开始链接

    为了将网页发布到web上,需要一个全日工作的网络服务器,最好的方法是找到一家主机代理商. 域名是用来定位网站的第一无二的名字. 网页的最顶层目录就是根目录.在Web服务器中,因为根目录中的东西有可能在 ...

  5. App之百度云推送

    集成SDK 下载最新的Android SDK压缩包并解压,在新建工程或已有工程中增加百度云推送功能. 我下载的是 ,里面有一个同名的文件夹,文件夹中有 导入云推送jar包和so文件: 将解压后的lib ...

  6. vimtutor

    ================================================================================ 欢 迎 阅 读 < V I M ...

  7. SVM神经网络的术语理解

    SVM(Support Vector Machine)翻译成中文是支持向量机, 这里的“机(machine,机器)”实际上是一个算法.而支持向量则是指那些在间隔区边缘的训练样本点[1]. 当初看到这个 ...

  8. 单片机与嵌入式 以及ARM DSP FPGA 几个概念的理解

    嵌入式设备一般要满足实时性的要求,而实时性是要求数据输入和输出的延时满足一定的要求.当然嵌入式一般都便携性都比PC要好,功能没有PC多,PC是通用,他是专用,一般只专注某些功能的实现,比如DSP专注数 ...

  9. php YAF

    Yaf 的特点: 用C语言开发的PHP框架, 相比原生的PHP, 几乎不会带来额外的性能开销. 所有的框架类, 不需要编译, 在PHP启动的时候加载, 并常驻内存. 更短的内存周转周期, 提高内存利用 ...

  10. 答CsdnBlogger问-关于职业发展和团队管理问题

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 问1:关于职业发展以及团队管理?(正能同學_) 请问在二线城市的小公司里,普通Android开发者的 ...