949. 给定数字能组成的最大时间

给定一个由 4 位数字组成的数组,返回可以设置的符合 24 小时制的最大时间。

最小的 24 小时制时间是 00:00,而最大的是 23:59。从 00:00 (午夜)开始算起,过得越久,时间越大。

以长度为 5 的字符串返回答案。如果不能确定有效时间,则返回空字符串。

 

示例 1:

输入:[1,2,3,4]
输出:"23:41"
示例 2:

输入:[5,5,5,5]
输出:""
 

提示:

A.length == 4
0 <= A[i] <= 9

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/largest-time-for-given-digits

class Solution:
    def largestTimeFromDigits(self, arr: List[int]) -> str:
        res=-1
        for h1,h2,m1,m2 in itertools.permutations(arr):
            hs=h1*10+h2
            ms=m1*10+m2 
            if hs*60+ms>res and hs<24 and ms<60:
               res=hs*60+ms
        return '{:02}:{:02}'.format(*divmod(res,60)) if res>=0 else ''
        

 check out https://www.cnblogs.com/xxxsans/p/13631447.html and https://www.cnblogs.com/xxxsans/p/13631414.html for more details

posted @ 2020-09-08 11:14  XXXSANS  阅读(210)  评论(0编辑  收藏  举报