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

     [暴力解法]:

    时间分析:n2

    空间分析:

    [思维问题]:

    不知道和回溯法有什么关系。一看特别麻烦,果断用暴力解法了

    [一句话思路]:

    用bitCount转化为二进制数

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    1. 表的小时不超过12
    2. String.format严格控制字符串格式

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    [复杂度]:Time complexity: O(n2) Space complexity: O(n)

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    麻烦

    [关键模板化代码]:

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

     [代码风格] :

    public class Solution {
        /*
         * @param : the number of "1"s on a given timetable
         * @return: all possible time
         */
        public List<String> readBinaryWatch(int num) {
            List<String> time = new ArrayList<String>();
            for (int h = 0; h < 12; h++) {//12 not 24
                for (int m = 0; m < 60; m++) {
                    if (Integer.bitCount(h) + Integer.bitCount(m) == num) {
                        time.add(String.format("%d:%02d", h, m));//String's strict format
                    }
                }
            }
            return time;
        }
    };
    View Code
  • 相关阅读:
    java-数组
    编程练习
    java-循环语句
    java-条件语句if&switch
    JAVA-运算符
    JAVA-常量
    springAOP源码分析之篇一:配置文件的解析
    spring IOC容器的扩展
    spring IOC之篇六 bean的加载---bean的创建
    spring IOC之篇五 bean的加载--缓存中获取
  • 原文地址:https://www.cnblogs.com/immiao0319/p/8532325.html
Copyright © 2020-2023  润新知