列表

详情


1736. 替换隐藏数字得到的最晚时间

给你一个字符串 time ,格式为 hh:mm(小时:分钟),其中某几位数字被隐藏(用 ? 表示)。

有效的时间为 00:0023:59 之间的所有时间,包括 00:0023:59

替换 time 中隐藏的数字,返回你可以得到的最晚有效时间。

 

示例 1:

输入:time = "2?:?0"
输出:"23:50"
解释:以数字 '2' 开头的最晚一小时是 23 ,以 '0' 结尾的最晚一分钟是 50 。

示例 2:

输入:time = "0?:3?"
输出:"09:39"

示例 3:

输入:time = "1?:22"
输出:"19:22"

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class Solution { public: string maximumTime(string time) { } };

python3 解法, 执行用时: 48 ms, 内存消耗: 16 MB, 提交时间: 2023-09-27 15:24:15

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

python3 解法, 执行用时: 44 ms, 内存消耗: 15.9 MB, 提交时间: 2023-09-27 15:23:52

class Solution:
    def maximumTime(self, time: str) -> str:
        time = list(time)
        for i in range(len(time)): 
            if time[i] == "?": 
                if i == 0: 
                    time[i] = "2" if time[i+1] in "?0123" else "1"
                elif i == 1: 
                    time[i] = "3" if time[0] == "2" else "9"
                elif i == 3: 
                    time[i] = "5"
                else: 
                    time[i] = "9"
                    
        return "".join(time)

php 解法, 执行用时: 8 ms, 内存消耗: 18.5 MB, 提交时间: 2023-09-27 15:23:21

class Solution {

    /**
     * @param String $time
     * @return String
     */
    function maximumTime($time) {
        //小时部分如果都是?,则取23点
        //如果只有一个?,则取值会受另一个值的限制
        if ($time[0] == '?' && $time[1] == '?') {
            $time[0] = '2';
            $time[1] = '3';
        } elseif ($time[0] == '?') {
            $time[0] = $time[1] > '3' ? '1' : '2';
        } elseif ($time[1] == '?') {
            $time[1] = $time[0] == '2' ? '3' : '9';
        }

        //分钟部分比较简单,直接取最大值即可
        if ($time[3] == '?') $time[3] = '5';
        if ($time[4] == '?') $time[4] = '9';

        return $time;
    }
}

javascript 解法, 执行用时: 64 ms, 内存消耗: 40.9 MB, 提交时间: 2023-09-27 15:21:30

/**
 * @param {string} time
 * @return {string}
 */
var maximumTime = function(time) {
    const arr = Array.from(time);
    if (arr[0] === '?') {
        arr[0] = ('4' <= arr[1] && arr[1] <= '9') ? '1' : '2';
    }
    if (arr[1] === '?') {
        arr[1] = (arr[0] == '2') ? '3' : '9';
    }
    if (arr[3] === '?') {
        arr[3] = '5';
    }
    if (arr[4] === '?') {
        arr[4] = '9';
    }
    return arr.join('');
};

java 解法, 执行用时: 0 ms, 内存消耗: 39.6 MB, 提交时间: 2023-09-27 15:21:15

class Solution {
    public String maximumTime(String time) {
        char[] arr = time.toCharArray();
        if (arr[0] == '?') {
            arr[0] = ('4' <= arr[1] && arr[1] <= '9') ? '1' : '2';
        }
        if (arr[1] == '?') {
            arr[1] = (arr[0] == '2') ? '3' : '9';
        }
        if (arr[3] == '?') {
            arr[3] = '5';
        }
        if (arr[4] == '?') {
            arr[4] = '9';
        }
        return new String(arr);
    }
}

cpp 解法, 执行用时: 4 ms, 内存消耗: 6.3 MB, 提交时间: 2023-09-27 15:21:00

class Solution {
public:
    string maximumTime(string time) {
        if (time[0] == '?') {
            time[0] = ('4' <= time[1] && time[1] <= '9') ? '1' : '2';
        }
        if (time[1] == '?') {
            time[1] = (time[0] == '2') ? '3' : '9';
        }
        if (time[3] == '?') {
            time[3] = '5';
        }
        if (time[4] == '?') {
            time[4] = '9';
        }
        return time;
    }
};

golang 解法, 执行用时: 0 ms, 内存消耗: 1.9 MB, 提交时间: 2021-07-02 10:29:43

func maximumTime(time string) string {
	arr := strings.Split(time, ":")
	hour, minute := []byte(arr[0]), []byte(arr[1])
	if hour[0] == '?' {
		if hour[1] < '4' || hour[1] > '9' {
			hour[0] = '2'
		} else {
			hour[0] = '1'
		}
	}
	if hour[1] == '?' {
		if hour[0]  < '2' {
			hour[1] = '9'
		} else {
			hour[1] = '3'
		}
	}
	
	if minute[0] == '?' {
		minute[0] = '5'
	}
	if minute[1] == '?' {
		minute[1] = '9'
	}
	return string(hour) + ":" + string(minute)
}

上一题