题目:

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.

思路:

这是一道很老的题了,没记错的话甚至谷歌的OA题库都有这个,LeetCode居然没有收录。首先思路要清晰,这题看上去复杂,但是限制挺多的,最大值是23:59,虽然不是一直能达到。首先判断前面两位,无非三种情况。一:双问号,那么直接都设成23即可;二:第一位是问号第二位不是,那么就看第二位是多少,如果大于3,比如5,那25:XX是不可能的,同时这里24:XX也是超过了最大值,不可能,因此如果第二位超过3,第一位最大只能是1,如果第二位小于等于3,第一位就可以是2,变成23:XX的情况;三:第一位不是问号,第二位是问号,同样的,判断第一位如果小于2,那么第二位可以是9,否则第二位最大只能是3,也是为了不超过最大值。然后再判断最后两位,即第四位,第五位(第三位是符号":")。如果第三位是问号,那么最大只能是5;第四位是问号,最大只能是9。这里因为我们是从前两位改到后两位,并不互相影响,所以直接在string原地改然后返回即可,不需要额外空间。

代码:

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

1736. Latest Time by Replacing Hidden Digits相关推荐

  1. LeetCode——1736. 替换隐藏数字得到的最晚时间(Latest Time by Replacing Hidden Digits)——分析及代码(Java)

    LeetCode--1736. 替换隐藏数字得到的最晚时间[Latest Time by Replacing Hidden Digits]--分析及代码[Java] 一.题目 二.分析及代码 1. 逐 ...

  2. javascript 手册

    目录 序言 准备 如何嵌入网页? 写在网页中 在链接上使用伪协议 使用独立的js脚本 语言核心 关于分号 输出Html最简单的方式 alert : 弹出窗口,调试必备 命名规则 基本类型 字符串 十六 ...

  3. scala字符串替换_如何在Scala中替换字符串中的正则表达式模式?

    scala字符串替换 Scala | 替换字符串中的正则表达式模式 (Scala | Replacing a regular expression pattern in a string) Repla ...

  4. Dealing-with-Missing-Data-in-Python

    文章目录 1. The problem with missing data 1.1 Why deal with missing data? 1.2 Steps for treating missing ...

  5. php basename读取中文,php basename处理中文的问题

    之前在处理文件上传的一个问题中,使用basename过滤的情况 // Remove path information and dots around the filename, to prevent ...

  6. Symantec:揭秘Hidden Lynx组织的APT***行动

    美国时间2013年9月17日,Symantec发布了一份名为<Hidden Lynx - Professional Hackers for Hire>的报告,揭示了Symantec多年来跟 ...

  7. 机器学习:手写数字识别(Hand-written digits recognition)小项目

    该项目的所有代码在我的github上,欢迎有兴趣的同学与我探讨研究~ 地址:Machine-Learning/machine-learning-ex3/ 1. Introduction 手写数字识别( ...

  8. Android Edittext digits 属性限制输入的内容

    digits属性中设置允许的字符,未允许的字符即使软键盘上有显示,点击后也无法显示到EditText中. 例如显示显示输入的内容为数字字母以及,和. 就可以使用如下 <EditTextandro ...

  9. python字符串常量_python教程---字符串常量ascii_letters、punctuation、digits、whitespace等...

    字符串常量 此模块中定义的常量为: string.ascii_letters 下文所述 ascii_lowercase 和 ascii_uppercase 常量的拼连. 该值不依赖于语言区域. str ...

最新文章

  1. 网站推广专员浅析关键词筛选决定网站推广的排名与流量
  2. mysql+index组合索引_MySQL 优化之 index merge(索引合并)
  3. [Windows][C#][.NET][WPF]基于ArcFace2.0+红外双目摄像头的活体检测
  4. linux img 内核启动,linux的启动流程(initrd.img)
  5. 关于delphi指针(转)
  6. 统计局:去年12月天然气、电力生产增长较快
  7. [独孤九剑]持续集成实践(二)– MSBuild语法入门
  8. OMG: daily scrum nine
  9. 《Python学习手册》——使用入门
  10. 四旋翼无人机数学模型推导
  11. tf.contrib.layers.embed_sequence()函数
  12. SkipList(跳跃表)详解
  13. 【王道】操作系统OS第三章存储系统(三)
  14. iOS-音频 + 视频
  15. UVALive 4043 Ants(最大权匹配)
  16. 如何使用kodi Mac安装中文插件
  17. Android常用开源库整理汇总
  18. 拜占庭将军问题 原文翻译
  19. 欧盟人工智能立法提案的核心思想 及未来影响分析
  20. chrome浏览器缓存视频_如何录制您的Chrome浏览器的视频

热门文章

  1. 人工智能产品经理知识体系和学习计划
  2. 云南通报5起森林火灾,如何去扑救?如何自救?
  3. 使用ajax提交form表单,包括ajax文件上传 转http://www.cnblogs.com/zhuxiaojie/p/4783939.html...
  4. CAD在画线的过程中显示长度和角度
  5. 创意简约土木黑灰配色PPT模板
  6. 首字母大写--C++实现
  7. php+科研课题管理 毕业设计-附源码170914
  8. mysql commit work_数据库commit work
  9. 华为安全 HCIP722笔记
  10. Hex Editor Neo Ultimate系统要求