zoukankan      html  css  js  c++  java
  • [LeetCode] 1736. Latest Time by Replacing Hidden Digits

    You are given a string time in the form of hh:mm, where some of the digits in the string are hidden (represented by ?).

    The valid times are those inclusively between 00:00 and 23:59.

    Return the latest valid time you can get from time by replacing the hidden digits.

    Example 1:

    Input: time = "2?:?0"
    Output: "23:50"
    Explanation: The latest hour beginning with the digit '2' is 23 and the latest minute ending with the digit '0' is 50.
    

    Example 2:

    Input: time = "0?:3?"
    Output: "09:39"
    

    Example 3:

    Input: time = "1?:22"
    Output: "19:22"

    Constraints:

    • time is in the format hh:mm.
    • It is guaranteed that you can produce a valid time from the given string.

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

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

    有效的时间为 00:00 到 23:59 之间的所有时间,包括 00:00 和 23:59 。

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

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/latest-time-by-replacing-hidden-digits
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    这是一道贪心 + 模拟的题。注意以下几点即可。需要对每个问号做出替换,替换成一个合法的数字。要根据问号出现的位置决定到底替换成什么数字才是合法的。

    如果不是问号,就直接 append 到结果;反之如果是问号,

    如果 i = 0,需要看一下 i = 1 位置上是否也是问号,如果是4 - 9之间的数字,那么只能配 0;如果是 1 - 3 之间的数字,可以配一个 2。

    如果 i = 1,需要看一下 i = 0 位置,如果是 0 或者 1,则配一个 9;如果是2,则只能配一个 3

    如果 i = 3或者4,则直接配 5 和 9 即可,因为分钟数最大是 59。

    时间O(n)

    空间O(1) - StringBuilder几乎可以忽略不计,因为空间是固定的

    Java实现

     1 class Solution {
     2     public String maximumTime(String time) {
     3         StringBuilder sb = new StringBuilder();
     4         int len = time.length();
     5         int index = 0;
     6         // corner case
     7         if (time.charAt(0) == '?' && time.charAt(1) == '?') {
     8             sb.append('2');
     9             sb.append('3');
    10             index += 2;
    11         }
    12 
    13         // normal case
    14         for (int i = index; i < len; i++) {
    15             if (time.charAt(i) == '?') {
    16                 if (i == 0) {
    17                     if (time.charAt(1) >= '0' && time.charAt(1) <= '3') {
    18                         sb.append('2');
    19                     } else {
    20                         sb.append('1');
    21                     }
    22                 } else if (i == 1) {
    23                     if (time.charAt(0) == '0' || time.charAt(0) == '1') {
    24                         sb.append('9');
    25                     } else if (time.charAt(0) == '2') {
    26                         sb.append('3');
    27                     }
    28                 } else if (i == 3) {
    29                     sb.append('5');
    30                 } else if (i == 4) {
    31                     sb.append('9');
    32                 }
    33             } else {
    34                 sb.append(time.charAt(i));
    35             }
    36         }
    37         return sb.toString();
    38     }
    39 }

    LeetCode 题目总结

  • 相关阅读:
    poj2452
    bnuoj16491
    1326: The contest(并查集+分组背包)
    BNUOJ-1065或运算的简单解法
    递推、规律思维题总结
    uva10160(dfs+状态压缩)
    第七章 人工智能,7.1 基于深度强化学习与自适应在线学习的搜索和推荐算法研究(作者:灵培、霹雳、哲予)
    第六章 大数据,6.3 突破传统,4k大屏的沉浸式体验(作者: 彦川、小丛)
    第六章 大数据,6.2 双11背后的大规模数据处理(作者:惠岸 朋春 谦乐)
    第六章 大数据,6.1 双11数据大屏背后的实时计算处理(作者:藏六 黄晓锋 同杰)
  • 原文地址:https://www.cnblogs.com/cnoodle/p/15056898.html
Copyright © 2011-2022 走看看