原题链接在这里: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的更多相关文章

  1. [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 ...

  2. [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 ...

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

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

  4. [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 ...

  5. [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 ...

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. clipbrd剪切板查看器

    本文,我们来学习一下简单的概念,即,如何查看系统剪贴版里面有什么内容?   如果要想看.或者验证系统剪贴版里面都有什么内容,最为简单的方法就是通过"粘贴"的操作来验证!   但是, ...

  2. Unix下 五种 I/O模型

    Unix下共有五种I/O模型: 1. 阻塞式I/O  2. 非阻塞式I/O  3. I/O复用(select和poll)  4. 信号驱动式I/O(SIGIO)  5. 异步I/O(POSIX的aio ...

  3. 《Python学习手册》(三)

    string 字符串,和列表.元组,都适用序列操作. 关于python转义字符 迭代 for x in S: print(x) [c * 2 for c in S] Comparasion: > ...

  4. JUnit4 入门笔记

    Test注解的两个可选参数 expected timeout The Test annotation supports two optional parameters. The first, expe ...

  5. HDU 1241 油田

    这道题明明很简单但不知道为什么运行结果一直错,但提交却是对的!代码真是神奇,不过我猜测可能是提上给出的数据错了,可能提上给的数据m和n后多给了一个空格或回车,但题的数据没有 #include<s ...

  6. SpringBoot2.0整合Sharding-Jdbc

    maven: <parent> <groupId>org.springframework.boot</groupId> <artifactId>spri ...

  7. poj2349 Arctic Network - 最小生成树

    2017-08-04 16:19:13 writer:pprp 题意如下: Description The Department of National Defence (DND) wishes to ...

  8. redis的Python接口调用

    Redis安装及教程: redis教程 安装Python的redis接口模块 redis-py requires a running Redis server. See redis教程 for ins ...

  9. scala学习手记8 - 自适应的默认做法

    scala有一些默认做法,会让代码更简洁.更易读写,下面列出了这样几个特性: 1. 支持脚本.scala支持脚本,因此无须将所有的代码都放到类里.如果脚本可以满足需求,就将代码放到一个脚本里,无须再创 ...

  10. ECS vs. Kubernetes 类似而又不同

    C2Container Service (ECS)和Kubernetes (K8s) 都解决了同样的问题:跨越主机集群管理容器.ECS和Kubernetes之间的斗争让我想起了vi和Emacs之间的编 ...