天天看點

LeetCode-1736. 替換隐藏數字得到的最晚時間

給你一個字元串 time ,格式為 hh:mm(小時:分鐘),其中某幾位數字被隐藏(用 ? 表示)。

有效的時間為 00:00 到 23:59 之間的所有時間,包括 00:00 和 23:59 。

替換 time 中隐藏的數字,傳回你可以得到的最晚有效時間。

tips:考慮所有情況就好了

class Solution:
    def maximumTime(self, time: str) -> str:
        time = list(time)
        if time[0] == '?' and time[1] != '?':
            if int(time[1]) <= 3:
                time[0] = '2'
            else:
                time[0] = '1'
        elif time[0] == '?' and time[1] == '?':
            time[0] = '2'
            time[1] = '3'
        if time[1] == '?' and time[0] == '0':
            time[1] = '9'
        elif time[1] == '?' and time[0] == '1':
            time[1] = '9'
        elif time[1] == '?' and time[0] == '2':
            time[1] = '3'
        if time[3] == '?':
            time[3] = '5'
        if time[4] == '?':
            time[4] = '9'
        return ''.join(time)