题目:

给定一个"HH:MM"格式的时间,重复使用这些数字,返回下一个最近的时间。每个数字可以被重复使用任意次。

保证输入的时间都是有效的。例如,"01:34","12:09" 都是有效的,而"1:34","12:9"都不是有效的时间。

样例

样例 1:

输入: "19:34"
输出: "19:39"
解释:
从1,9,3,4中选出的下一个最近的时间是19:39,它是五分钟后。
答案不是19:33,因为它是23小时59分钟后。

样例 2:

输入: "23:59"
输出: "22:22"
解释: 可以假设所返回的时间是第二天的时间,因为它在数字上小于给定的时间。

分析:

LeetCode681需要订阅才可以查看,好在LintCode上有相同的题目。

给定一个"HH:MM"格式的时间,利用所有数字拼凑出新的时间(可以重复),使其成为下一个最近的时间,且保证时间有效。

特例是像"00:00"的最近时间就是"00:00",实际上是过了24小时。

可以将每一位的数字替换成给的时间内的四个数字(最多4个),总计最多4^4种情况,同时检验时间是否有效,小时小于24,分钟小于60。

然后计算拼出的时间和原时间的差值,求得最小的即可,注意求差时容易出现负值,要处理一下。

程序:

C++

class Solution {
public:
/**
* @param time: the given time
* @return: the next closest time
*/
string nextClosestTime(string &time) {
// write your code here
vector<int> number {time[]-'', time[]-'', time[]-'', time[]-''} ;
vector<int> currTime(, );
int hour = number[]* + number[];
int minute = number[]* + number[];
int times = toMinute(hour, minute);
int bestTime = times;
dfs(number, , currTime, times, bestTime);
string s = to_string(bestTime/) + ":" + to_string(bestTime%);
char buff[];
sprintf(buff, "%02d:%02d", bestTime / , bestTime % );
return string(buff);
}
void dfs(vector<int> &number, int deep, vector<int>& currTime, int times, int& bestTime){
if(deep == ){
if(currTime[]* + currTime[] > || currTime[]* + currTime[] > )
return;
int currMinutes = toMinute(currTime[]* + currTime[], currTime[]* + currTime[]);
if(diff(times, currMinutes) < diff(times, bestTime)){
bestTime = currMinutes;
}
return;
}
for(int i:number){
currTime[deep] = i;
dfs(number, deep+, currTime, times, bestTime);
}
return;
}
int toMinute(int hour, int minute){
return hour* + minute;
}
int diff(int time1, int time2){
if(time1 == time2)
return ;
return (( - time1) + time2) % ;
}
};

Java

public class Solution {
/**
* @param time: the given time
* @return: the next closest time
*/
public String nextClosestTime(String time) {
// write your code here
char tTime[] = time.toCharArray();
int[] number = new int[]{tTime[0]-'0', tTime[1]-'0', tTime[3]-'0', tTime[4]-'0'};
int[] currTime = new int[4];
int hour = number[0]*10 + number[1];
int minute = number[2]*10 + number[3];
int times = toMinute(hour, minute);
bestTime = times;
dfs(number, currTime, 0, times);
String res = String.format("%02d:%02d", bestTime/60, bestTime%60);
return res;
}
public void dfs(int[] number, int[] currTime, int deep, int times){
if(deep == 4){
if(currTime[0]*10+currTime[1] > 23 || currTime[2]*10+currTime[3] > 59)
return;
int currMinutes = toMinute(currTime[0]*10+currTime[1], currTime[2]*10+currTime[3]);
if(diff(times, currMinutes) < diff(times, bestTime)){
bestTime = currMinutes;
}
return;
}
for(int i = 0; i < 4; ++i){
currTime[deep] = number[i];
dfs(number, currTime, deep+1, times);
}
return;
}
public int toMinute(int hour, int minute){
return hour*60 + minute;
}
public int diff(int time1, int time2){
if(time1 == time2)
return 1440;
return ((1440 - time1) + time2) % 1440;
}
private int bestTime;
}

LeetCode 681. Next Closest Time 最近时刻 / LintCode 862. 下一个最近的时间 (C++/Java)的更多相关文章

  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 681. Next Closest Time

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

  3. 当有多于64合乎逻辑的cpu时刻,Windows 下一个Oracle db 实例启动(startup)什么时候会hang(待定)

    Bug 9772171 - Database startup hangs on Windows when machine has more than 64 cores [ID 9772171.8] 该 ...

  4. LeetCode (13): 3Sum Closest

    https://leetcode.com/problems/3sum-closest/ [描述] Given an array S of n integers, find three integers ...

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

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

  6. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

  7. LeetCode——16. 3Sum Closest

    一.题目链接:https://leetcode.com/problems/3sum-closest/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出3个数来,使得这三个数的 ...

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

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

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

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

随机推荐

  1. mysql 执行计划查看

    使用explain关键字可以模拟优化器执行SQL查询语句,从而知道MySQL是如何处理你的SQL语句的,分析你的查询语句或是表结构的性能瓶颈.explain执行计划包含的信息 其中最重要的字段为:id ...

  2. es lucene搜索及聚合流程源码分析

    本文以TermQuery,GlobalOrdinalsStringTermsAggregator为例,通过代码,分析es,lucene搜索及聚合流程.1:协调节点收到请求后,将search任务发到相关 ...

  3. 小白学Java:包装类

    目录 小白学Java:包装类 包装类的继承关系 创建包装类实例 自动装箱与拆箱 自动装箱 自动拆箱 包装类型的比较 "=="比较 equals比较 自动装箱与拆箱引发的弊端 自动装 ...

  4. # go微服务框架kratos学习笔记六(kratos 服务发现 discovery)

    目录 go微服务框架kratos学习笔记六(kratos 服务发现 discovery) http api register 服务注册 fetch 获取实例 fetchs 批量获取实例 polls 批 ...

  5. Java之String类用法总结

    String类概述: 1.String类代表字符串.Java 程序中的所有字符串字面值(如"abc")都作为此类的实例实现. 2.String是一个final类,代表不可变的字符序 ...

  6. PQSQL 按照时间进行分组

    按照时间分组时一般是按照年.月.日进行分组,不会把时分秒也算进去,所以需要把时间戳提取出所需要的时间段,本质上是把时间戳格式化成对应形式的字符串,这个过程需要用to_char(timestamp, t ...

  7. next_permutation 函数

    next_permutation 是一个定义在 <algorithm> 中的一个全排列函数, 用于按顺序生成一个数列的全排列 基本用法 : int a[] = {1, 2, 3}; do{ ...

  8. Microsoft Azure Storage Explorer(2)

    之前写过一个往Microsoft Azure Storage Explorer里存储的功能,现在又要把东西给下载下来. 记录一下: public string DownFileFromAzure() ...

  9. document.visibilityState 监听浏览器

    document.hidden:表示页面是否隐藏的布尔值.页面隐藏包括 页面在后台标签页中 或者 浏览器最小化 (注意,页面被其他软件遮盖并不算隐藏,比如打开的 sublime 遮住了浏览器). do ...

  10. Spring(二)核心容器 - 简介 、BeanFactory、ApplicationContext

    目录 前言 1.容器简介 2.容器的结构 2.1 BeanFactory 2.2 ApplicationContext 2.2.1 ConfigurableApplicationContext 2.2 ...