LeetCode 681. Next Closest Time 最近时刻 / LintCode 862. 下一个最近的时间 (C++/Java)
题目:
给定一个"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)的更多相关文章
- [LeetCode] 681. Next Closest Time 下一个最近时间点
Given a time represented in the format "HH:MM", form the next closest time by reusing the ...
- leetcode 681. Next Closest Time
Given a time represented in the format "HH:MM", form the next closest time by reusing the ...
- 当有多于64合乎逻辑的cpu时刻,Windows 下一个Oracle db 实例启动(startup)什么时候会hang(待定)
Bug 9772171 - Database startup hangs on Windows when machine has more than 64 cores [ID 9772171.8] 该 ...
- LeetCode (13): 3Sum Closest
https://leetcode.com/problems/3sum-closest/ [描述] Given an array S of n integers, find three integers ...
- [LeetCode] Next Closest Time 下一个最近时间点
Given a time represented in the format "HH:MM", form the next closest time by reusing the ...
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- LeetCode——16. 3Sum Closest
一.题目链接:https://leetcode.com/problems/3sum-closest/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出3个数来,使得这三个数的 ...
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- LeetCode 31. Next Permutation (下一个排列)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
随机推荐
- docker 修改实例名称
docker 容器(服务)重命名只要一个命令就可以: docker rename 原容器名 新容器名 如:
- Linux下扫描服务器IP地址是否冲突(arp-scan)
部署服务突然发现,连接的服务器断开了,因为服务器用户名密码是一样的,所以重新连接后,发现文件变了,跟之前不一样. 猜想是不是ip地址冲突了,两次连接的服务器不同. 网上查找资料说可以用工具扫描.工具: ...
- cogs 1588. [USACO Feb04]距离咨询 倍增LCA
1588. [USACO Feb04]距离咨询 ★★ 输入文件:dquery.in 输出文件:dquery.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] 农夫 ...
- Spring学习记录3——Spring AOP
SpringAOP基础 AOP简介: AOP是Aspect Oriented Programing的简称,翻译为“面向切面编程”.它适用于具有横切逻辑的应用场合,如性能检测,访问控制,事务管理及日志记 ...
- Webpack实战(六):如何优雅地运用样式CSS预处理
上一篇文章中,我主要分享了<Webpack如何分离样式文件>CSS 预处理器是一个能让你通过预处理器自己独有的语法来生成CSS的程序,css预处理指的是在开发中我们经常会使用一些样式预编译 ...
- restframework 认证、权限、频率组件
一.认证 1.表的关系 class User(models.Model): name = models.CharField(max_length=32) pwd = models.CharField( ...
- Java入门 - 高级教程 - 08.Applet
原文地址:http://www.work100.net/training/java-applet.html 更多教程:光束云 - 免费课程 Applet 序号 文内章节 视频 1 概述 2 Apple ...
- 大叔 Frameworks.Entity.Core 3 Predicate
Frameworks.Entity.Core\Commons\Predicate\ 1LinqEntity.cs /// IQueryable扩展方法:条件过滤与排序功能 /// Modify ...
- robotframework从列表中循环读取数据,传入关键字执行
场景预设:从列表内读取手机号,自动化执行微信加好友,直至选择完所有数据后,脚本停止执行 1.建一个备选数据表,表内列待添加的手机号数据 2.脚本的主要流程新加好友-输入手机号-添加好友-判断好友是否存 ...
- Java基础系列2:深入理解String类
Java基础系列2:深入理解String类 String是Java中最为常用的数据类型之一,也是面试中比较常被问到的基础知识点,本篇就聊聊Java中的String.主要包括如下的五个内容: Strin ...