zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    word删除脚注的方法
    移动最小二乘
    word插入的对象和文字混合到了一起的解决办法
    插值
    中学小学学校学生德育量化管理系统_文明班评比量化系统_德育评价系统_德育量化考核系统_政教管理系统_政教考核系统
    需求的最初形式:12306ng的需求小说
    异步javascript的原理和实现
    需求与设计过程(1)用例
    项目管理沙龙第十二次会议纪要为没有共识的项目组定制敏捷方法
    项目进度控制的技术
  • 原文地址:https://www.cnblogs.com/immiao0319/p/8532325.html
Copyright © 2011-2022 走看看