给你一个字符串 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”
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/latest-time-by-replacing-hidden-digits
public static String maximumTime(String time) {
StringBuilder str = new StringBuilder();
for(int i =0;i<time.length();i++){
switch (i){
case 0:
if(time.charAt(i) == '?'&&time.charAt(i+1) != '?'){
if(Integer.parseInt(String.valueOf(time.charAt(i+1)))>3){
str.append('1');
}else{
str.append('2');
}
}else{
if(time.charAt(i) == '?'){
str.append('2');
}else {
str.append(time.charAt(i));
}
}
break;
case 1:
if(time.charAt(i-1)=='?'||time.charAt(i-1)=='2'){
if(time.charAt(i) == '?'){
str.append('3');
}else{
str.append(time.charAt(i));
}
}else {
if(time.charAt(i) == '?'){
str.append('9');
}else{
str.append(time.charAt(i));
}
}
str.append(':');
break;
case 3:
if(time.charAt(i) == '?'){
str.append('5');
}else{
str.append(time.charAt(i));
}
break;
case 4:
if(time.charAt(i) == '?'){
str.append('9');
}else{
str.append(time.charAt(i));
}
break;
}
}
return str.toString();
}