99 - 二进制手表

题目

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

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

img

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

给定一个非负整数 n 代表当前 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”。

解答

这题目给我看傻了,题解也看傻了

作者:ljj666

链接:https://leetcode-cn.com/problems/binary-watch/solution/cjian-jian-dan-dan-de-ji-xing-dai-ma-jie-jue-wen-t/

Runtime: 56 ms, faster than 66.18% of JavaScript online submissions for Binary Watch.

Memory Usage: 34.3 MB, less than 100.00% of JavaScript online submissions for Binary Watch.

其中的count1()是用来计算二进制中1的个数

n&(n-1)能清除二进制最低位的1

Runtime: 0 ms, faster than 100.00% of Go online submissions for Binary Watch.

Memory Usage: 2.9 MB, less than 100.00% of Go online submissions for Binary Watch.

用过从int转string,总是忘记用这个函数😂😂,然后就会转出一堆乱码。。

Runtime: 44 ms, faster than 24.96% of Python3 online submissions for Binary Watch.

Memory Usage: 13.8 MB, less than 9.09% of Python3 online submissions for Binary Watch.

需要在代码当中导入List这个类型,不然会报错。。

python的三元运算符。。😂

递归回溯

作者:WiseMove 链接:https: // leetcode - cn.com / problems / binary - watch / solution / python3di - gui - hui - su - by - wisemove /

我一开始看到手表的想法也是这个思路,但怎么都写不出来。。

还是这个大佬厉害。。

Runtime: 40 ms, faster than 56.81% of Python3 online submissions for Binary Watch.

Memory Usage: 14 MB, less than 9.09% of Python3 online submissions for Binary Watch.

暴力美学

作者:SherryOKOK 链接:https: // leetcode - cn.com / problems / binary - watch / solution / cheng - xu - yuan - de - bao - li - mei - xue - by - sherryokok /

简单题解决不花里胡哨😂笑死了

Runtime: 40 ms, faster than 56.81% of Python3 online submissions for Binary Watch.

Memory Usage: 13.7 MB, less than 9.09% of Python3 online submissions for Binary Watch.

Last updated

Was this helpful?