LeetCode Next Closest Time
原题链接在这里:https://leetcode.com/problems/next-closest-time/
题目:
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:
Input: "19:34"
Output: "19:39"
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:
Input: "23:59"
Output: "22:22"
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.
题解:
用当前time已经出现的四个数字,组成新的time. 找diff最小的新time.
First get integer hash value of current time.
Get all 4 digits of current time. Take 2 of them to construct hour, and the other 2 to construct minute.
Use new time hash value - current time hash value and maintain the minimum diff.
If minused result < 0, it is next day, add it with 24 * 60.
Time Complexity: O(1). 共有4^4种组合.
Space: O(1). size为4的HashSet.
AC Java:
class Solution {
public String nextClosestTime(String time) {
Set<Integer> hs = new HashSet<>();
for(int i = 0; i<time.length(); i++){
char c = time.charAt(i);
if(c != ':'){
hs.add(c-'0');
}
}
int cur = Integer.valueOf(time.substring(0, 2)) * 60 + Integer.valueOf(time.substring(3, 5));
int minDiff = 24*60;
// res is initialized as time, in case "11:11". The expected result is "11: 11".
String res = time;
for(int h1 : hs){
for(int h2 : hs){
if(h1*10 + h2 < 24){
for(int m1 : hs){
for(int m2 : hs){
if(m1*10 + m2 < 60){
int can = (h1*10+h2) * 60 + m1*10+m2;
int diff = can-cur;
if(diff < 0){
diff += 24*60;
}
// diff can't 0, otherwise, it would return itself
if(diff>0 && diff<minDiff){
minDiff = diff;
res = ""+h1+h2+":"+m1+m2;
System.out.println("m1: "+ m1 + " m2: " + m2);
}
}
}
}
}
}
}
return res;
}
}
LeetCode Next Closest Time的更多相关文章
- [LeetCode] 272. Closest Binary Search Tree Value II 最近的二叉搜索树的值 II
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- [LeetCode] 3Sum Closest 最近三数之和
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- [LeetCode] Next Closest Time 下一个最近时间点
Given a time represented in the format "HH:MM", form the next closest time by reusing the ...
- [LeetCode] 272. Closest Binary Search Tree Value II 最近的二分搜索树的值之二
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- [LeetCode] 270. Closest Binary Search Tree Value 最近的二叉搜索树的值
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
- Leetcode 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- Leetcode 270. Closest Binary Search Tree Value
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
- [LeetCode#272] Closest Binary Search Tree Value II
Problem: Given a non-empty binary search tree and a target value, find k values in the BST that are ...
- [leetcode]272. Closest Binary Search Tree Value II二叉搜索树中最近的值2
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
随机推荐
- Python数据处理实例
使用python进行数据处理的实例(数据为某公司HR部门关于员工信息的部分摘录,kaggle上面的一次赛题) https://www.kaggle.com/c/kfru-dbm-hr-analytic ...
- 苹果Dock样式的菜单
在线演示 本地下载
- 解决Vim插入模式下backspace按键无法删除字符的问题【转】
本文转载自:https://blog.csdn.net/zxy987872674/article/details/64124959 最近使用某个服务器编辑文件时,快捷键i进入插入模式后,下方不出现in ...
- HTTP与抓包
HTTP就是超文本传输协议,底层使用socket TCP长连接,基于请求与响应,是同步请求. socket 绝对多数语言都是支持socket的,底层走的是二进制传输. HTTP协议实际上是对Socke ...
- Java通过JDBC操作Hive
http://www.cnblogs.com/netbloomy/p/6688670.html 0.概述 使用的都是CLI或者hive –e的方式仅允许使用HiveQL执行查询.更新等操作.然而Hiv ...
- PHP模拟登录发送闪存
url,post,cookie. 有这三种就可以了. 下面使用Postman模拟发送. 其中,body中是post参数,header中是cookie数据. 下面是php模拟代码. public fun ...
- numpy数组各种乘法
In [34]: a Out[34]: array([[1, 4], [5, 6]]) In [35]: b Out[35]: array([[4, 1], [2, 2]]) In [36]: np. ...
- istringstream 用法
istringstream 类用于执行C++风格的串流的输入操作 istringstream用空格作为字符串分隔符 #include <iostream>#include <sstr ...
- 20165332实验三 敏捷开发与XP实践
20165332实验三 敏捷开发与XP实践 实验内容 1:XP基础 2:XP核心实践 3:相关工具 实验1 在IDEA中使用工具(Code->Reformate Code)把下面代码重新格式化, ...
- WPF中的平移缩放和矩阵变换(TranslateTransform、ScaleTransform、MatrixTransform)
在WPF中的平移缩放都是通过RenderTransform这个类来实现这些效果的,在这个类中,除了平移和缩放还有旋转.扭曲变换.矩阵变换.这些都差不多的,都是坐标的变换. 这里我就先简单弄个平移和缩放 ...