Given a time represented in the format "HH:MM", form the next closest time by reusing the current digits. There is no limit on how many times a digit can be reused.

You may assume the given input string is always valid. For example, "01:34", "12:09" are all valid. "1:34", "12:9" are all invalid.

Example 1:

  1. Input: "19:34"
  2. Output: "19:39"
  3. Explanation: The next closest time choosing from digits 1, 9, 3, 4, is 19:39, which occurs 5 minutes later. It is not 19:33, because this occurs 23 hours and 59 minutes later.

Example 2:

  1. Input: "23:59"
  2. Output: "22:22"
  3. Explanation: The next closest time choosing from digits 2, 3, 5, 9, is 22:22. It may be assumed that the returned time is next day's time since it is smaller than the input time numerically.

这道题给了我们一个时间点,让我们求最近的下一个时间点,规定了不能产生新的数字,当下个时间点超过零点时,就当第二天的时间。为了找到下一个时间点,我们肯定是从分钟开始换数字,而且换的数字要是存在的数字,那么我们最先要做的就是统计当前时间点中的数字,由于可能有重复数字的存在,我们把数字都存入集合set中,这样可以去除重复数字,并且可以排序,然后再转为vector。下面就从低位分钟开始换数字了,如果低位分钟上的数字已经是最大的数字了,那么说明要转过一轮了,就要把低位分钟上的数字换成最小的那个数字;否则就将低位分钟上的数字换成下一个数字。然后再看高位分钟上的数字,同理,如果高位分钟上的数字已经是最大的数字,或则下一个数字大于5,那么直接换成最小值;否则就将高位分钟上的数字换成下一个数字。对于小时位上的数字也是同理,对于小时低位上的数字情况比较复杂,当小时高位不为2的时候,低位可以是任意数字,而当高位为2时,低位需要小于等于3。对于小时高位,其必须要小于等于2,参见代码如下:

解法一:

  1. class Solution {
  2. public:
  3. string nextClosestTime(string time) {
  4. string res = time;
  5. set<int> s{time[], time[], time[], time[]};
  6. string str(s.begin(), s.end());
  7. for (int i = res.size() - ; i >= ; --i) {
  8. if (res[i] == ':') continue;
  9. int pos = str.find(res[i]);
  10. if (pos == str.size() - ) {
  11. res[i] = str[];
  12. } else {
  13. char next = str[pos + ];
  14. if (i == ) {
  15. res[i] = next;
  16. return res;
  17. } else if (i == && next <= '') {
  18. res[i] = next;
  19. return res;
  20. } else if (i == && (res[] != '' || (res[] == '' && next <= ''))) {
  21. res[i] = next;
  22. return res;
  23. } else if (i == && next <= '') {
  24. res[i] = next;
  25. return res;
  26. }
  27. res[i] = str[];
  28. }
  29. }
  30. return res;
  31. }
  32. };

下面这种方法的写法比较简洁,实际上用了暴力搜索,由于按分钟算的话,一天只有1440分钟,也就是1440个时间点,我们可以从当前时间点开始,遍历一天的时间,也就是接下来的1440个时间点,得到一个新的整型时间点后,我们按位来更新结果res,对于每个更新的数字字符,看其是否在原时间点字符中存在,如果不存在,直接break,然后开始遍历下一个时间点,如果四个数字都成功存在了,那么将当前时间点中间夹上冒号返回即可,参见代码如下:

解法二:

  1. class Solution {
  2. public:
  3. string nextClosestTime(string time) {
  4. string res = "";
  5. vector<int> v{, , , };
  6. int found = time.find(":");
  7. int cur = stoi(time.substr(, found)) * + stoi(time.substr(found + ));
  8. for (int i = , d = ; i <= ; ++i) {
  9. int next = (cur + i) % ;
  10. for (d = ; d < ; ++d) {
  11. res[d] = '' + next / v[d];
  12. next %= v[d];
  13. if (time.find(res[d]) == string::npos) break;
  14. }
  15. if (d >= ) break;
  16. }
  17. return res.substr(, ) + ":" + res.substr();
  18. }
  19. };

参考资料:

https://discuss.leetcode.com/topic/104692/c-java-clean-code

https://discuss.leetcode.com/topic/104736/concise-java-solution

https://discuss.leetcode.com/topic/105411/short-simple-java-using-regular-expression

 

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Next Closest Time 下一个最近时间点的更多相关文章

  1. [LeetCode] 681. Next Closest Time 下一个最近时间点

    Given a time represented in the format "HH:MM", form the next closest time by reusing the ...

  2. [LeetCode] 31. Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  3. Leetcode题库——31.下一个排列

    @author: ZZQ @software: PyCharm @file: nextPermutation.py @time: 2018/11/12 15:32 要求: 实现获取下一个排列的函数,算 ...

  4. leetCode 31.Next Permutation (下一个字典序排序) 解题思路和方法

    Next Permutation  Implement next permutation, which rearranges numbers into the lexicographically ne ...

  5. [leetcode]31. Next Permutation下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  6. LeetCode 681. Next Closest Time 最近时刻 / LintCode 862. 下一个最近的时间 (C++/Java)

    题目: 给定一个"HH:MM"格式的时间,重复使用这些数字,返回下一个最近的时间.每个数字可以被重复使用任意次. 保证输入的时间都是有效的.例如,"01:34" ...

  7. [LeetCode] Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  8. 获取当前时间UTC时间的下一个15分钟时间点

    ZonedDateTime zdt = ZonedDateTime.now(ZoneOffset.UTC); int now15Minute = zdt.getMinute() / P15MINUTE ...

  9. LeetCode 31. Next Permutation (下一个排列)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

随机推荐

  1. JavaEE Servlet 核心方法及生命周期

    做JavaWeb开发,免不了要和Servlet打交道.Servlet是Sun(Oracle)官方定义的一个Web开发规范,所有Servlet开发都必须遵守.自己以前也没有从头做过Web开发,所以这方面 ...

  2. 20162323周楠《Java程序设计与数据结构》第八周总结

    20162323周楠 2016-2017-2 <程序设计与数据结构>第八周学习总结 教材学习内容总结 一个异常是一个对象,它定义了并不轻易出现的或是错误的情形 异常由程序或运行时环境抛出, ...

  3. SpringMVC 无法访问到指定jsp页面可能的原因

    当出现你的程序可以访问到对应的controller层.但是却无法访问对应的jsp文件时.你首先做的不是检查web.xml等配置文件,而是打开的服务器根文件检查对应路径下的文件是否存在.命名是否正确.命 ...

  4. [笔试题目]使用Stringbuffer无 参的构造函数创建 一个对象时,默认的初始容量是多少? 如果长度不够使用了,自动增长多少倍?

    [笔试题目] 使用Stringbuffer无 参的构造函数创建 一个对象时,默认的初始容量是多少? 如果长度不够使用了,自动增长多少倍? StringBuffer 底层是依赖了一个字符数组才能存储字符 ...

  5. javascript原型链__proto__属性的理解

    在javascript中,按照惯例,构造函数始终都应该以一个大写字母开头,而非构造函数则应该以一个小写字母开头.一个方法使用new操作符创建,例如下面代码块中的Person1(可以吧Person1看做 ...

  6. PHP类的自动加载

    spl_autoload_register(function ($className) { require str_replace('\\', '/', $className '.php'); }) ...

  7. (转载) ASP.NET(C#) Web Api 通过文件流下载文件到本地实例

    下载文件到本地是很多项目开发中需要实现的一个很简单的功能.说简单,是从具体的代码实现上来说的,.NET的文件下载方式有很多种,本示例给大家介绍的是ASP.NET Web Api方式返回HttpResp ...

  8. Linq 集合操作符 Except,Intersect,Union

    IList<string> s1 = new List<string>() { "One", "Two", "Three&qu ...

  9. vueJs 源码解析 (三) 具体代码

    vueJs 源码解析 (三) 具体代码 在之前的文章中提到了 vuejs 源码中的 架构部分,以及 谈论到了 vue 源码三要素 vm.compiler.watcher 这三要素,那么今天我们就从这三 ...

  10. LINGO 基础学习笔记

    LINGO 中建立的优化模型可以由5个部分组成,或称为 5 段(section): (1)集合段(SETS):这部分要以"SETS:"开始,以"ENDSETS" ...