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. Java多态性理解

      Java中多态性的实现 什么是多态 面向对象的三大特性:封装.继承.多态.从一定角度来看,封装和继承几乎都是为多态而准备的.这是我们最后一个概念,也是最重要的知识点. 多态的定义:指允许不同类的对 ...

  2. AP_总体业务及方案

    AP关键业务点说明 关键业务点 说明 预付款余额收回 1. 在应付款管理系统中输入一张虚拟发票,该发票的目的是在系统中冲减对供应商的预付款额,其金额等于预付款的未核销金额,供应商为原供应商. 借:其他 ...

  3. day11

    JSP入门   1 JSP概述 1.1 什么是JSP JSP(Java Server Pages)是JavaWeb服务器端的动态资源.它与html页面的作用是相同的,显示数据和获取数据.   1.2 ...

  4. 使用Reveal

    添加Reveal.framework,设置Other link flags 添加Debug为 -ObjC,添加 libz 库 这里介绍 Reveal UI 分析工具的简单使用,至于使用他分析手机 Ap ...

  5. Docker Device Mapper 使用 direct-lvm

      一.Device Mapper: loop-lvm 默认 CentOS7 下 Docker 使用的 Device Mapper 设备默认使用 loopback 设备,后端为自动生成的稀疏文件,如下 ...

  6. ViewModel在MVC3中的应用:实现多字段表格的部分更新

    假设我们有这样一张用户表: public class F_users { [Key] [Display(Name="用户名:")] [Required(ErrorMessage=& ...

  7. JQuery直接调用asp.net后台WebMethod方法

    利用JQuery的$.ajax()可以很方便的调用asp.net的后台方法.[WebMethod]   命名空间 1.无参数的方法调用, 注意:1.方法一定要静态方法,而且要有[WebMethod]的 ...

  8. NAT123 解决80端口被封的问题

    使用的服务器不知什么原因80端口无法使用了,好像是被封了,用的移动的固定IP,移动线路一直是不稳定 关键是移动的回答竟然是找不到哪里封的 是不是被屏蔽了,无奈使用了NAT123做处理.试了下还是管用. ...

  9. 修改OpenCart系统配置

    后台修改admin配置文件和修改根目录下的config.php <?php// HTTPdefine('HTTP_SERVER', 'http://网站域名/');define('HTTP_IM ...

  10. LightOj1190 - Sleepwalking(判断点与多边形的位置关系--射线法模板)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1190 题意:给你一个多边形含有n个点:然后又m个查询,每次判断点(x, y)是否在多边 ...