Skip to content

401 binary watch

401. Binary Watch

题目: https://leetcode.com/problems/binary-watch/

难度: Easy

思路:

一看到位操作,我的内心是拒绝的。

我也有想这样的想法,因为其实可以的组合并没有那么多,干脆枚举算了,然而也没有动手来写,直到被发了题解的截屏。

class Solution(object):
    def readBinaryWatch(self, num):
        """
        :type num: int
        :rtype: List[str]
        """
        hour = { 0 : ['0'],
        1:['1','2','4','8'],
        2:['3','5','6','9','10'],
        3:['7','11']
        }

        minute = { 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']
        }

        res = []

        #num = num for hour + num for minute
        i = 0

        while i <= 3 and i <= num:
            if num - i <= 5:
                for str1 in hour[i]:
                    for str2 in minute[num-i]:
                        res.append(str1 + ':' + str2)
            i += 1
        return res

关于循环那处,因为hour的led最多只有4个,所以这样写循环



回到顶部