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的更多相关文章

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

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

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

  4. ICP算法(Iterative Closest Point迭代最近点算法)

    标签: 图像匹配ICP算法机器视觉 2015-12-01 21:09 2217人阅读 评论(0) 收藏 举报 分类: Computer Vision(27) 版权声明:本文为博主原创文章,未经博主允许 ...

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

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

  7. K closest points

    Find the K closest points to a target point in a 2D plane. class Point { public int x; public int y; ...

  8. 16. 3Sum Closest

    题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  9. jquery:closest和parents的主要区别

    closest和parents的主要区别是:1,前者从当前元素开始匹配寻找,后者从父元素开始匹配寻找:2,前者逐级向上查找,直到发现匹配的元素后就停止了,后者一直向上查找直到根元素,然后把这些元素放进 ...

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

随机推荐

  1. 坚果Pro2刷魔趣系统教程,刷回锤子系统教程

    一.刷魔趣系统 1.高通驱动安装 https://blog.csdn.net/qq_43653944/article/details/86702169 2.刷入twrp rec https://blo ...

  2. 灰度图像--图像增强 非锐化掩蔽 (Unsharpening Mask)

    学习DIP第35天 转载请标明本文出处:http://blog.csdn.net/tonyshengtan,欢迎大家转载,发现博客被某些论坛转载后,图像无法正常显示,无法正常表达本人观点,对此表示很不 ...

  3. Android_(消息提示)多种使用Toast的消息提示

    Toast是一种提供给用户简介信息的视图,可以创建和显示消息,该视图以浮于应用程序之上的形式呈现给用户.因为它并不获得焦点,即使用户正在输入什么也不会受到影响. Toast目标是尽可能以不显眼的方式, ...

  4. vue-property-decorator知识梳理

    仓库地址: /* npm 仓库地址 */ // https://www.npmjs.com/package/vue-property-decorator /* github地址 */ // https ...

  5. [题解] [TJOI2011] 构造矩阵

    题面 题解 很容易看出来是道网络流的题目, 要是没有这个字典序最小, 直接建图跑一遍就好了, 考虑如何输出字典序最小的方案 我们可以贪心地去选择, 若当前点可以选0就选0, 不能选0就选1, 有一点像 ...

  6. leetcode题目1.两数之和(简单)

    给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元 ...

  7. Java - 自动装箱与拆箱详解

    1.装箱与拆箱 装箱,将基本数据类型转为包装类型.拆箱,将包装类型转为基本数据类型. // Byte, Short, Integer, Long, Double, Float, Boolean, Ch ...

  8. Mac 10.15 关闭SIP

    升级Mac后SIP开启了,根目录不能创建文件了 关闭 sip,终端输入 sudo mount -uw / 在我们开发过程中,有时候我们安装一些工具软件需要将文件拷贝到系统限制更改的文件夹中,甚至有时需 ...

  9. vxWorks下常用的几种延时方法

         在应用编程的时候,通常会碰到需要一个任务在特定的延时之后执行一个指定的动作,如等待外设以确保数据可靠,控制扬声器发声时间以及串口通信超时重发等.这就需要利用定时器机制来计量特定长度的时间段. ...

  10. sklearn4_混合分类器

    python机器学习-乳腺癌细胞挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003&u ...