• LeetCode--401--二进制手表


    问题描述:

    二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59)

    每个 LED 代表一个 0 或 1,最低位在右侧。

    例如,上面的二进制手表读取 “3:25”。

    给定一个非负整数 代表当前 LED 亮着的数量,返回所有可能的时间。

    案例:

    输入: n = 1
    返回: ["1:00", "2:00", "4:00", "8:00", "0:01", "0:02", "0:04", "0:08", "0:16", "0:32"]

    注意事项:

    • 输出的顺序没有要求。
    • 小时不会以零开头,比如 “01:00” 是不允许的,应为 “1:00”。
    • 分钟必须由两位数组成,可能会以零开头,比如 “10:2” 是无效的,应为 “10:02”。

    方法:

     1 class Solution(object):
     2     def readBinaryWatch(self, num):
     3         """
     4         :type num: int
     5         :rtype: List[str]
     6         """
     7         res = []
     8         for hour in range(12):
     9             for minute in range(60):
    10                 if (bin(hour) + bin(minute)).count('1') == num:
    11                     res.extend(["%d:%02d"%(hour,minute)])
    12         return res

    官方:

     1 class Solution(object):
     2     def readBinaryWatch(self, num):
     3         """
     4         :type num: int
     5         :rtype: List[str]
     6         """
     7         a={0: ['0'], 1: ['1', '2', '4', '8'], 2: ['3', '5', '6', '9', '10'], 3: ['7', '11']}
     8         b={0: ['00'], 1: ['01', '02', '04', '08', '16', '32'], 2: ['03', '05', '06', '09', '10', '12', '17', '18', '20', '24', '33', '34', '36', '40', '48'], 3: ['07', '11', '13', '14', '19', '21', '22', '25', '26', '28', '35', '37', '38', '41', '42', '44', '49', '50', '52', '56'], 4: ['15', '23', '27', '29', '30', '39', '43', '45', '46', '51', '53', '54', '57', '58'], 5: ['31', '47', '55','59']}
     9         if num<0 or num>8:
    10             return []
    11         mi=max(0,num-5)
    12         mx=min(3,num)
    13         res=[]
    14         for i in range(mi,mx+1):
    15             for x in a[i]:
    16                 for y in b[num-i]:
    17                     res.append(x+':'+y)
    18         return res
  • 相关阅读:
    一 数据库备份与恢复 2 数据库恢复 2.2 数据库重定向与重建
    附录 常用SQL语句 Dynamic SQL
    alt_disk_install 克隆系统rootvg
    Mysql版本升级
    DB29.7 HADR环境升级
    EMC VNX系列存储维护
    保存最开始的flink code,  数据是自动生成而不是通过kafka
    opentsdb restful api使用方法
    flink 和 hbase的链接
    opentsdb
  • 原文地址:https://www.cnblogs.com/NPC-assange/p/9725183.html
Copyright © 2020-2023  润新知