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.
public class Test {
public String nextClosestTime(String time) {
char[] res = time.toCharArray();
char[] digits = new char[] { res[], res[], res[], res[] };
Arrays.sort(digits);
// 从右到左对res进行操作,只要有当前最小单位时间的替换,返回替换后的时间
res[] = findNext(digits, res[], '');
if (res[] > time.charAt()) return String.valueOf(res);
res[] = findNext(digits, res[], '');
if (res[] > time.charAt()) return String.valueOf(res);
res[] = res[] == '' ? findNext(digits, res[], '') : findNext(digits, res[], '');
if (res[] > time.charAt()) return String.valueOf(res);
res[] = findNext(digits, res[], '');
return String.valueOf(res);
}
private char findNext(char[] digits, char cur, char upper) {
if (cur == upper) return digits[];
// 找到cur的位置,然后加1得到下一个位置
int pos = Arrays.binarySearch(digits, cur) + ;
// 如果下一个位置的数还是原来的数,或者超过了上限数,前进到再下一个
while (pos < && (digits[pos] == cur || digits[pos] > upper)) {
pos++;
}
return pos == ? digits[] : digits[pos];
}
}
Next Closest Time的更多相关文章
- [LeetCode] 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] 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 ...
- ICP算法(Iterative Closest Point迭代最近点算法)
标签: 图像匹配ICP算法机器视觉 2015-12-01 21:09 2217人阅读 评论(0) 收藏 举报 分类: Computer Vision(27) 版权声明:本文为博主原创文章,未经博主允许 ...
- Leetcode 16. 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:3Sum, 3Sum Closest, 4Sum
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
- K closest points
Find the K closest points to a target point in a 2D plane. class Point { public int x; public int y; ...
- 16. 3Sum Closest
题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- jquery:closest和parents的主要区别
closest和parents的主要区别是:1,前者从当前元素开始匹配寻找,后者从父元素开始匹配寻找: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 ...
随机推荐
- gdb插件gef安装爬坑
0x00: gdb是linux下的调试利器,但无奈界面不太友好,所以需要一些辅助插件. 0x01:关于插件选择 之前我一直使用的是pead,之前了解到还有个插件gef,因为gef支持多构架,而且hea ...
- 实用——pojo (实体类) 常用函数
1,转义方法 @JsonProperty("n")
- maven项目创7 配置分页插件
页面编写顺序 首先确定是否拥有想要的pojo(对象实体类)———>dao层mybatis配置——>service层的接口及实现类——>controller(web下) 分页插件作 ...
- Confluence 6.15 锚点(Anchor)宏
允许你链接到页面的特定部分.有关如何使用锚点的内容,请参考页面 Anchors 页面中的详细内容. Wiki 标记(markup) 示例 宏名称: anchor 宏内容:None. {anchor:h ...
- Qbxt 模拟题 day2(am) T2 jian
[问题描述] 有N个数,随机选择一段区间,如果这段区间的所有数的平均值在[L,R]中则你比较厉害.求你比较厉害的概率. [输入格式] 第一行有三个数N, l, r,含义如上描述. 接下来一行有N个数代 ...
- noi.ac#458 sequence
题目链接:戳我 蒟蒻的第一道子序列自动机! 给定两个01串A,B,求一个最短的01串,要求C不是A,B的子序列.要求如果同样短,输出字典序最小的. 那么我们先构建A,B两个串的子序列自动机.然后我们设 ...
- AtCoder AGC009E Eternal Average (DP)
题目链接 https://atcoder.jp/contests/agc009/tasks/agc009_e 题解 又被劝退了... 第一步转化非常显然: 就等价于一开始有一个数\(1\), 有\(\ ...
- Gitlab启动、停止、重启(两种启动方式)
因为Gitlab不是我部署的,是之前总监部署的,服务器突然更新系统了,Git服务器就没有自启··自启··自启······,自己操作启动没有成功,然后网上搜了一下都是这三种启动关闭重启的方式,可是我这里 ...
- 初学 Nginx (一) SSI 的作用
SSI:Server Side Include,是一种基于服务端的网页制作技术, Nginx ssi 的例子如下: It took a little while to figure this out ...
- [心得]暑假DAY1 | 7-7考试总结
呼.. 正式开始暑假集训. 今天一上午还在搞7-7的考试改题 然而,该来该去,TLE48过不去了 不知道哪的问题,loj上1w3ms(卡常都没能救得了) 至于T1和T3,简单总结一下算了 排序 感觉很 ...