zoukankan      html  css  js  c++  java
  • 【leetcode】949. Largest Time for Given Digits

    题目如下:

    Given an array of 4 digits, return the largest 24 hour time that can be made.

    The smallest 24 hour time is 00:00, and the largest is 23:59.  Starting from 00:00, a time is larger if more time has elapsed since midnight.

    Return the answer as a string of length 5.  If no valid time can be made, return an empty string.

    Example 1:

    Input: [1,2,3,4]
    Output: "23:41"
    

    Example 2:

    Input: [5,5,5,5]
    Output: ""
    

    Note:

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

    解题思路:就四个数字,把所有组合方式列举一遍,求最大值即可。

    代码如下:

    class Solution(object):
        def largestTimeFromDigits(self, A):
            """
            :type A: List[int]
            :rtype: str
            """
            import itertools
            res = -1
            for i in itertools.permutations(A,4):
                if i[0]*10 + i[1] < 24 and i[2]*10 + i[3] < 60:
                    res = max(res,(i[0]*1000+i[1]*100+i[2]*10+i[3]))
            if res == -1:
                return  ''
            res = str(res).zfill(4)
            return res[0:2] + ':' + res[2:]
  • 相关阅读:
    HTMLDOM
    换行
    【iOS】APP之数据存储
    开启远程XUL
    iOS之Streams
    Plugin的生命周期
    ActiveX Control Test Container
    ObjectiveC Runtime III【objc_msgSend函数】
    What is a Digital Signature?
    JS变量作用域
  • 原文地址:https://www.cnblogs.com/seyjs/p/10058403.html
Copyright © 2011-2022 走看看