問題描述:
二進位制手錶頂部有 4 個 led 代表小時(0-11),底部的 6 個 led 代表分鐘(0-59)。
每個 led 代表乙個 0 或 1,最低位在右側。
例如,上面的二進位制手錶讀取 「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"]
1官方:class
solution(object):
2def
readbinarywatch(self, num):
3"""
4:type num: int
5:rtype: list[str]
6"""
7 res =
8for hour in range(12):
9for minute in range(60):
10if (bin(hour) + bin(minute)).count('
1') ==num:
11 res.extend(["
%d:%02d
"%(hour,minute)])
12return res
1class
solution(object):
2def
readbinarywatch(self, num):
3"""
4:type num: int
5:rtype: list[str]
6"""
7 a=
8 b=
9if num<0 or num>8:
10return
11 mi=max(0,num-5)
12 mx=min(3,num)
13 res=
14for i in range(mi,mx+1):
15for x in
a[i]:
16for y in b[num-i]:
:'+y)
18return res
LeetCode 401 二進位制手錶
二進位制手錶頂部有 4 個 led 代表小時 0 11 底部的 6 個 led 代表分鐘 0 59 每個 led 代表乙個 0 或 1,最低位在右側。解題思路 我自己想到的解法是用dfs去搜尋滿足條件的組合,然後看完 william 用暴力解法,解法也比較好,這裡都記錄一下。先看自己的解法。我的解法...
leetcode 401 二進位制手錶
思路一 遞迴回溯 使用乙個長度為10的陣列進行組合編碼,最終輸出時間 思路二 兩重for迴圈分別代表小時和分鐘 class solution for int i index ireadbinarywatch int num class solution return ret 1.bitset bit...
leetcode 401 二進位制手錶
401 二進位制手錶 algorithms easy 48.85 likes 73 dislikes 0 total accepted 5.2k total submissions 10.6k testcase example 0 二進位制手錶頂部有 4 個 led 代表小時 0 11 底部的 6 ...