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 ...
随机推荐
- #define只有一个参数
define后面只有一个名字,那么这个宏是空的,不会对代码产生影响. 用来便于阅读的
- 广度优先搜索 BFS算法
广度优先搜索算法(Breadth-First-Search,BFS),又称作宽度优先搜索.BFS算法是从根节点开始,沿着树的宽度遍历树的节点.如果所有节点均被访问,则算法中止. 算法思想 1.首先将根 ...
- BigDecimal相关整理
bigdecimal类型四则运算: BigDecimal s = new Bigdecimal(5); BigDecimal x = new Bigdecimal(15); 依次为最基础的加减乘除: ...
- COS-6主存管理
操作系统是用户和计算机的接口,同时也是计算机硬件和其他软件的接口.操作系统的功能包括管理计算机系统的硬件.软件及数据资源,控制程序运行,改善人机界面,为其它应用软件提供支持,让计算机系统所有资源最大限 ...
- 使用axis2构建webservice
axis2是可以实现webservice的一个插件,使用这个插件可以发布webservice 1:可以使用这个插件来发布webservice,可以看网址:http://clq9761.iteye.co ...
- AtCoder Regular Contest 097
AtCoder Regular Contest 097 C - K-th Substring 题意: 求一个长度小于等于5000的字符串的第K小子串,相同子串算一个. K<=5. 分析: 一眼看 ...
- 安装pysqlite2
1. 从https://github.com/msabramo/pysqlite2 下载源码. 2.安装python-dev: sudo apt-get install python-dev 否则在 ...
- Android -- 工程架构,电话拨号器, 点击事件的4中写法
(该系列整理自张泽华android视频教程) 1. android工程 各个文件夹的作用 src/ java原代码存放目录 gen/ 自动生成目录 gen 目录中存放所有由Android开发工具自动 ...
- 表格布局tabelLayout
表格布局tabelLayout 一.简介 二.实例 <!-- 这个tableRow里面有两个组件,所以是两列 --> <!-- 这个tableRow里面有三个组件,所以是三列 --& ...
- Pycharm for mac 快捷键
cmd b 跳转到声明处(cmd加鼠标) opt + 空格 显示符号代码 (esc退出窗口 回车进入代码) cmd []光标之前/后的位置 opt + F7 find usage cmd backsp ...