-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path401.Binary-Watch.py
50 lines (38 loc) · 1.18 KB
/
401.Binary-Watch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# https://leetcode.com/problems/binary-watch/
#
# algorithms
# Easy (45.81%)
# Total Accepted: 69,493
# Total Submissions: 151,697
class Solution(object):
def readBinaryWatch(self, num):
"""
:type num: int
:rtype: List[str]
"""
hour = [1, 2, 4, 8]
minute = [1, 2, 4, 8, 16, 32]
res = []
h_arr, m_arr = [], []
def recursive(arr, idx, length, tmp, res_arr):
arr_len = len(arr)
if length == 0:
res_arr.append(tmp)
return
if idx >= arr_len:
return
for i in xrange(idx, arr_len):
recursive(arr, i + 1, length - 1, tmp + arr[i], res_arr)
for i in xrange(0, num + 1):
h_arr, m_arr = [], []
recursive(hour, 0, i, 0, h_arr)
recursive(minute, 0, num - i, 0, m_arr)
for h in h_arr:
if h > 11:
continue
for m in m_arr:
if m > 59:
continue
tmp = str(h) + ':' + ('0' if m < 10 else '') + str(m)
res.append(tmp)
return res