给你一个字符串 time
,格式为 hh:mm
(小时:分钟),其中某几位数字被隐藏(用 ?
表示)。
有效的时间为 00:00
到 23:59
之间的所有时间,包括 00:00
和 23:59
。
替换 time
中隐藏的数字,返回你可以得到的最晚有效时间。
示例 1:
输入:time = "2?:?0" 输出:"23:50" 解释:以数字 '2' 开头的最晚一小时是 23 ,以 '0' 结尾的最晚一分钟是 50 。
示例 2:
输入:time = "0?:3?" 输出:"09:39"
示例 3:
输入:time = "1?:22" 输出:"19:22"
提示:
time
的格式为hh:mm
- 题目数据保证你可以由输入的字符串生成有效的时间
public class Solution1736 {
public String maximumTime(String time) {
String h1 = "";
String h2 = "";
String m1 = "";
String m2 = "";
h1 = time.substring(0, 1);
h2 = time.substring(1, 2);
m1 = time.substring(3, 4);
m2 = time.substring(4, 5);
if (h1.equals("?")) {
if (h2.equals("?")) {
h1 = "2";
h2 = "3";
} else {
if (Integer.parseInt(h2) < 4) {
h1 = "2";
} else {
h1 = "1";
}
}
}
if (h2.equals("?")) {
if (h1.equals("?")) {
h1 = "2";
h2 = "3";
} else {
if (Integer.parseInt(h1) < 2) {
h2 = "9";
} else {
h2 = "3";
}
}
}
if (m1.equals("?")) {
m1 = "5";
}
if (m2.equals("?")) {
m2 = "9";
}
return h1 + h2 + ":" + m1 + m2;
}
public static void main(String[] args) {
Solution1736 s = new Solution1736();
String time = "2?:?0";
System.out.println(s.maximumTime(time));
}
}