|
| 1 | +class Solution(object): |
| 2 | + def combination(self, n, k, inp, res): |
| 3 | + # inp is expected to be a list of size n |
| 4 | + #print n,k,inp,res |
| 5 | + if k == 0: |
| 6 | + res.append([i for i in inp]) |
| 7 | + return |
| 8 | + if k > n: |
| 9 | + return |
| 10 | + #for i in range(1,n+1): |
| 11 | + inp[n-1] = 1 |
| 12 | + self.combination(n-1,k-1, inp, res) |
| 13 | + inp[n-1] = 0 |
| 14 | + self.combination(n-1, k, inp, res) |
| 15 | + |
| 16 | + |
| 17 | + def getHours(self, h): |
| 18 | + if h == 0: |
| 19 | + return ["0"] |
| 20 | + if h == 1: |
| 21 | + return ["1", "2", "4", "8"] |
| 22 | + if h == 2: |
| 23 | + return ["3", "5", "6", "9", "10"] |
| 24 | + if h == 3: |
| 25 | + return ["7", "11"] |
| 26 | + |
| 27 | + def getMinutes(self, m): |
| 28 | + inp = [0,0,0,0,0,0] |
| 29 | + res = [] |
| 30 | + self.combination(6, m, inp, res) |
| 31 | + mins = [1, 2, 4, 8, 16, 32] |
| 32 | + minutes = [] |
| 33 | + for comb in res: |
| 34 | + i = 0 |
| 35 | + mn = 0 |
| 36 | + while i < 6: |
| 37 | + if comb[i] == 1: |
| 38 | + mn += mins[i] |
| 39 | + i += 1 |
| 40 | + if mn > 59: |
| 41 | + continue |
| 42 | + if mn < 10: |
| 43 | + minutes.append("0" + str(mn)) |
| 44 | + else: |
| 45 | + minutes.append(str(mn)) |
| 46 | + return minutes |
| 47 | + |
| 48 | + def readBinaryWatch(self, num): |
| 49 | + """ |
| 50 | + :type num: int |
| 51 | + :rtype: List[str] |
| 52 | + """ |
| 53 | + i = 0 |
| 54 | + res = [] |
| 55 | + |
| 56 | + while i <= num: |
| 57 | + h = i |
| 58 | + m = num - i |
| 59 | + if h < 4 and m < 6: |
| 60 | + lh = self.getHours(h) |
| 61 | + #print lh |
| 62 | + lm = self.getMinutes(m) |
| 63 | + #print lm |
| 64 | + for hr in lh: |
| 65 | + for mn in lm: |
| 66 | + res.append(hr + ":" + mn) |
| 67 | + i += 1 |
| 68 | + |
| 69 | + return res |
0 commit comments