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 ...
随机推荐
- echo指令
1.在Linux中echo命令用来在标准输出上显示一段字符,比如:echo "the echo command test!" 这个就会输出“the echo command tes ...
- Eclipse 启动tomcat 访问主页报错404
问题 tomcat用startup.sh启动,访问localhost:8080能正常访问,用Eclipse service启动tomcat,访问localhost:8080报错404 解决方法 1. ...
- BZOJ-5424: 烧桥计划(单调队列)
BZOJ-5424: 烧桥计划(单调队列) 题目链接 题解: 先考虑最暴力的\(dp\):设\(f[k][i]\)表示搞掉第\(1\sim i\)段,烧了\(k\)段的最小花费,设\(calc(x,y ...
- BZOJ 3329 Xorequ:数位dp + 矩阵快速幂
传送门 题意 现有如下方程:$ x \oplus 3x = 2x $ 其中 $ \oplus $ 表示按位异或. 共 $ T $ 组数据,每组数据给定正整数 $ n $,任务如下: 求出小于等于 $ ...
- 在oracle中插入数据报错:ORA-00984列在此处不允许
这里报错的原因就是当数据类型varchar2时没有使用单引号. 没写单引号,不管是双引号还是什么都没写都会报这个错误.
- Java class、Object、Class 的区别
Java的对象模型中: 所有的类都是Class类的实例,Object是类,那么Object也是Class类的一个实例. 所有的类都最终继承自Object类,Class是类,那么Class也继承自Obj ...
- ubuntu 编译 vim+lua
mac $ brew install vim --with-cscope --with-lua --override-system-vim 安装spf13-vim见下面linux部分. 如果安装mac ...
- MySQL忘记密码解决方案
1.修改本地mysql目录中的my.ini文件 添加skip-grant-tables 2.在win +r 输入cmd,进行mysql的重启启动操作 net stop MySQL 停止服务 ...
- 如何退出telnet
ctrl键+ENter键 然后输入 进入telnet 命令 quit
- 【Java】对象的创建过程
一.对象的创建过程 1.首次创建对象时或该类静态方法/静态域首次被访问时,JAVA解释器查找该类的路径,定位该类的class文件 2.载入该类的class文件,有关静态初始化的所有动作执行,但是只执行 ...