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

This commit is contained in:
huangge1199@hotmail.com 2021-07-24 19:24:57 +08:00
parent ea4cf9f54d
commit 85f486ad83
4 changed files with 114 additions and 2 deletions

View File

@ -0,0 +1,73 @@
//给你一个字符串 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
// 题目数据保证你可以由输入的字符串生成有效的时间
//
// Related Topics 字符串
// 👍 44 👎 0
package leetcode.editor.cn;
//1736:替换隐藏数字得到的最晚时间
class LatestTimeByReplacingHiddenDigits {
public static void main(String[] args) {
//测试代码
Solution solution = new LatestTimeByReplacingHiddenDigits().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
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);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,39 @@
<p>给你一个字符串 <code>time</code> ,格式为 <code> hh:mm</code>(小时:分钟),其中某几位数字被隐藏(用 <code>?</code> 表示)。</p>
<p>有效的时间为 <code>00:00</code><code>23:59</code> 之间的所有时间,包括 <code>00:00</code><code>23:59</code></p>
<p>替换 <code>time</code> 中隐藏的数字,返回你可以得到的最晚有效时间。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>time = "2?:?0"
<strong>输出:</strong>"23:50"
<strong>解释:</strong>以数字 '2' 开头的最晚一小时是 23 ,以 '0' 结尾的最晚一分钟是 50 。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>time = "0?:3?"
<strong>输出:</strong>"09:39"
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>time = "1?:22"
<strong>输出:</strong>"19:22"
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>time</code> 的格式为 <code>hh:mm</code></li>
<li>题目数据保证你可以由输入的字符串生成有效的时间</li>
</ul>
<div><div>Related Topics</div><div><li>字符串</li></div></div>\n<div><li>👍 44</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long