博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
读二进制表的显示 Binary Watch
阅读量:7233 次
发布时间:2019-06-29

本文共 3705 字,大约阅读时间需要 12 分钟。

  hot3.png

问题:

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.

25204944_gmpo.jpg

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 = 1Return: ["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".

解决:

【注】

关于回溯法

① 使用回溯的思想,分别在小时和分钟上做DFS(DFS可以看作是回溯的一种),给定几个灯亮,然后把这些亮的灯枚举分给小时和分钟.需要注意的是剪枝,即小时必须小于12,分钟小于60.然后将小时和分钟组合即可.还有一个需要注意的是如果分钟只有1位数,还要补0。

public class Solution { // 3ms

    public List<String> readBinaryWatch(int num) {
        List<String> res = new ArrayList<>();
        for (int i = Math.max(0,num - 6);i <= Math.min(4,num) ;i ++ ) {
            List<Integer> list1 = new ArrayList<>();
            List<Integer> list2 = new ArrayList<>();
            DFS(4,i,0,0,list1);
            DFS(6,num - i,0,0,list2);
            for (int n1 : list1) {
                for (int n2 : list2)
{
                    String str = (String.valueOf(n2).length() == 1 ? "0" : "") + String.valueOf(n2);
                    res.add(String.valueOf(n1) + ":" + str);
                }
            }
        }
        return res;
    }
    public void DFS(int len,int k,int curIndex,int val,List<Integer> list){
        if(k == 0 && len == 4 && val < 12) list.add(val);
        if(k == 0 && len == 6 && val < 60) list.add(val);
        if(curIndex == len || k == 0) return; //停止条件
        DFS(len,k,curIndex + 1,val,list);
        val += Math.pow(2,curIndex);//计算第curIndex代表的值
        k --;
        curIndex ++;
        DFS(len,k,curIndex,val,list); //
    }
}

② 在discuss中看到,将所有的小时和分钟的可能性用数组列出来,以空间换时间,耗时少。

public class Solution { //2ms

        String[][] hour = {
{"0"},  // hours contains 0 1's
                   {"1", "2", "4", "8"},   // hours contains 1 1's
                   {"3", "5", "6", "9", "10"},  // hours contains 2 1's
                   {"7", "11"}};  // hours contains 3 1's
        String[][] minute = {
{"00"},  // mins contains 0 1's
                         {"01", "02", "04", "08", "16", "32"},  // mins contains 1 1's
                         {"03", "05", "06", "09", "10", "12", "17", "18", "20", "24", "33", "34", "36", "40", "48"},  // mins contains 2 1's
                         {"07", "11", "13", "14", "19", "21", "22", "25", "26", "28", "35", "37", "38", "41", "42", "44", "49", "50", "52", "56"},  // mins contains 3 1's
                         {"15", "23", "27", "29", "30", "39", "43", "45", "46", "51", "53", "54", "57", "58"},  // mins contains 4 1's
                         {"31", "47", "55", "59"}};  // mins contains 5 1's  
                     
    public List<String> readBinaryWatch(int num) {
        List<String> res = new ArrayList();
         // loop from 0 to 3 which is the max number of bits can be set in hours (4 bits)
        for (int i = 0; i <= 3 && i <= num; i++) { 
            // this if condition is to make sure the index from minutes array would be valid
            if (num - i <= 5) {
                // if we have i 1's in hours, then we need num - i 1's in minutes, that's why the arrays were created by grouping the number of 1's bits
                for (String str1 : hour[i]) {
                    for (String str2 : minute[num - i]) {
                        res.add(str1 + ":" + str2);
                    }
                }
            }
        }
        return res;     
    }
}

使用Bit Manipulation来解决,1,2,4,8都是2的整数倍,它们的二进制都只包含一个1,所以搜索所有的解空间,看哪几个数的bit之和等于num。使用Integer.bitCount()来计算1的个数,这个方法既好理解,又好使用。

public class Solution { // 3ms                  

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

 

转载于:https://my.oschina.net/liyurong/blog/1511172

你可能感兴趣的文章
JavaScript 浅谈 button 和 input type=button 的区别
查看>>
未知领域
查看>>
混合云存储组合拳:基于云存储网关与混合云备份的OSS数据备份方案
查看>>
APP实现再次查询功能
查看>>
痛失移动支付的翼支付,未来仍存四大机会?
查看>>
移动电竞迅猛崛起背后满满都是痛点?
查看>>
入门tensorflow.js,写一个双色求预测程序!梦想要有的,万一中了呢!
查看>>
Vue生命周期activated之返回上一页不重新请求数据
查看>>
windows下实用工具推荐
查看>>
为什么那么多自学Python的后来都放弃了,原因。
查看>>
rsync同步和备份文件到本地
查看>>
pygame.error: font not initialized的解决及init()到底干了什么
查看>>
ApacheCN 翻译活动进度公告 2019.2.18
查看>>
在VUE中利用MQTT协议实现即时通讯
查看>>
React入门:从零搭建一个React项目
查看>>
golang 之 import 和 package 的使用
查看>>
Python之父重回决策层,社区未来如何发展?
查看>>
J2EE开发笔记(一)—— J2EE开发环境配置
查看>>
算法与数据结构大系列 - NO.1 - 插入排序
查看>>
SWF是什么文件,SWF文件用什么软件可以打开
查看>>