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 ...
随机推荐
- Saltstack sls文件:批量添加删除计划任务
一.使用sls文件 批量添加计划任务 1.创建salt目录 mkdir /srv/{salt,pillar} 2.再salt目录下创建文件 vim crontab.sls 3.添加内容 # 添加的命令 ...
- ios 中实现storyboard 与xib 之间的切换
1,跳转到xib 假设有一个按钮,这个按钮就是实现跳转的,那么在这个按钮的点击事件中,代码可以这样写. AViewController *a1= [[AViewController alloc]ini ...
- adplayer移植【转】
本文转载自:https://blog.csdn.net/qq361294382/article/details/50525412 这两天做madplayer移植,由于是刚装的ubuntu14.04,所 ...
- Android中获取屏幕高度和宽度
有时我们需要获取当前屏幕的高度和宽度,只需要在一个Activity的onCreate()方法中写上如下代码即可: //定义DisplayMetrics 对象 DisplayMetrics metric ...
- Pandas统计函数
统计方法有助于理解和分析数据的行为.现在我们将学习一些统计函数,可以将这些函数应用到Pandas的对象上. pct_change()函数 系列,DatFrames和Panel都有pct_change( ...
- SpringBoot实现文件上传功能
新建maven项目,pom文件: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="htt ...
- 新版appium绘制九宫格的一个注意点
在用appium-desktop-setup-1.6.2进行app手势密码设置时,发现move_to(x, y)相对偏移量的方法用不了,绘制的手势也是乱跑 还会抛一个错误 selenium.commo ...
- 音悦台mv视频下载
需要获取的页面: 参考了此处,做了修改,代码如下: #coding:utf-8 import urllib2 import urllib import re import sys import os ...
- vim编辑16进制
你可以在vim中可以把文件转换为16进制来显示: :%!xxd 解释:把所有的行(%)用本地(!)的xxd程序打开. xxd本是linux下一个显示.编辑.转换二进制的命令. 返回正常显示: :%!x ...
- SQL Server中的联合主键、聚集索引、非聚集索引
我们都知道在一个表中当需要2列以上才能确定记录的唯一性的时候,就需要用到联合主键,当建立联合主键以后,在查询数据的时候性能就会有很大的提升,不过并不是对联合主键的任何列单独查询的时候性能都会提升,但我 ...