1. 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).
  2.  
  3. Each LED represents a zero or one, with the least significant bit on the right.
  4.  
  5. For example, the above binary watch reads "3:25".
  6.  
  7. Given a non-negative integer n which represents the number of LEDs that are currently on, return all possible times the watch could represent.
  8.  
  9. Example:
  10.  
  11. Input: n = 1
  12. Return: ["1:00", "2:00", "4:00", "8:00", "0:01", "0:02", "0:04", "0:08", "0:16", "0:32"]
  13. Note:
  14. The order of output does not matter.
  15. The hour must not contain a leading zero, for example "01:00" is not valid, it should be "1:00".
  16. 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()

  1. public class Solution {
  2. public List<String> readBinaryWatch(int num) {
  3. List<String> res = new ArrayList<String>();
  4. for (int i=0; i<12; i++) {
  5. for (int j=0; j<60; j++) {
  6. if (Integer.bitCount(i) + Integer.bitCount(j) == num) {
  7. String str1 = Integer.toString(i);
  8. String str2 = Integer.toString(j);
  9. res.add(str1 + ":" + (j<10? "0"+str2 : str2));
  10. }
  11. }
  12. }
  13. return res;
  14. }
  15. }

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

  1. public class Solution {
  2. public List<String> readBinaryWatch(int num) {
  3. int[] nums1 = new int[]{8, 4, 2, 1}, nums2 = new int[]{32, 16, 8, 4, 2, 1};
  4. List<String> res = new ArrayList<String>();
  5. for (int i=0; i<=num; i++) {
  6. List<Integer> hours = getTime(nums1, i, 12);
  7. List<Integer> minutes = getTime(nums2, num-i, 60);
  8. for (int hour : hours) {
  9. for (int minute : minutes) {
  10. res.add(hour + ":" + (minute<10? "0"+minute : minute));
  11. }
  12. }
  13. }
  14. return res;
  15. }
  16.  
  17. public List<Integer> getTime(int[] nums, int count, int limit) {
  18. List<Integer> res = new ArrayList<Integer>();
  19. getTimeHelper(res, count, 0, 0, nums, limit);
  20. return res;
  21. }
  22.  
  23. public void getTimeHelper(List<Integer> res, int count, int pos, int sum, int[] nums, int limit) {
  24. if (count == 0) {
  25. if (sum < limit)
  26. res.add(sum);
  27. return;
  28. }
  29. for (int i=pos; i<nums.length; i++) {
  30. getTimeHelper(res, count-1, i+1, sum+nums[i], nums, limit);
  31. }
  32. }
  33. }

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. VPN服务器环境搭建

    一.VPN服务器环境说明 操作系统:CentOS release 6.4 (Final) 本地网卡: 复制代码 代码如下: # ifconfig em1 Link encap:Ethernet HWa ...

  2. 【转】四元数(Quaternion)和旋转

    四元数介绍 旋转,应该是三种坐标变换--缩放.旋转和平移,中最复杂的一种了.大家应该都听过,有一种旋转的表示方法叫四元数.按照我们的习惯,我们更加熟悉的是另外两种旋转的表示方法--矩阵旋转和欧拉旋转. ...

  3. OAuth的机制原理讲解及开发流程

    本想前段时间就把自己通过QQ OAuth1.0.OAuth2.0协议进行验证而实现QQ登录的心得及Demo实例分享给大家,可一直很忙,今天抽点时间说下OAuth1.0协议原理,及讲解下QQ对于Oaut ...

  4. 权限管理:(RBAC)

    一般做正规的权限管理程序基本就是以下M表模式: 例1:在页面显示管理者的权限,并可以修改的管理界面 数据库表如下: 管理界面(附ajax): <body> <?php include ...

  5. OC文件大小的计算方法,多用于清理缓存

    OC文件大小的计算方法,一般多用于清理缓存.下载.统计 可以使用以下方法: #pragma mark Bt转换 + (NSString *)axcFileSizeWithToString:(unsig ...

  6. MongoDB的find用法

    0.查询符合条件数据的总条数 如:db.list名.find({条件}).count(); 1.返回指定的键值:db.list.find({条件},{name:"任意值",age: ...

  7. 在sublime中使用less

    高亮显示: 可以在Less文件中显示语法高亮,这样看起来会更舒服一些. 按下Ctrl+Shift+P调出命令面板:输入install调出Install Package选项并回车:输入less,选中并安 ...

  8. Linux中的ps命令

    Linux中ps命令用来列出系统中当前运行的那些进程. 使用格式:ps 参数   如:ps -A 通过man ps可以获得ps的详细参数用法 -A 显示所有进程信息 c 列出程序时,显示每个程序真正的 ...

  9. android常用命令

    首先配置好环境变量会比较方便... 大部分sdk提供的工具都在sdk\platform-tools和sdk\tools下,建议配置这两个路径到path 另外aapt工具在sdk\build-tools ...

  10. Spring @ResponseBody只能返回String类型数据解决办法

    今天自己搭Spring MVC框架玩,使用AJAX调用Spring controller 并返回map对象,突然发现,哎,怎么@Response中只能返回String, 我用的Spring 3的版本也 ...