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 ...
随机推荐
- jquery trigger() 方法 语法
jquery trigger() 方法 语法 作用:trigger() 方法触发被选元素的指定事件类型.深圳大理石平台 触发事件:规定被选元素要触发的事件. 语法:$(selector).trigge ...
- luogu 1072 Hankson 的趣味题 唯一分解定理+线性筛
貌似是比大多数题解优的 $O(n^2logn)$ ~ Code: #include <bits/stdc++.h> #define N 50004 #define setIO(s) fre ...
- Android_(控件)使用ListView显示Android系统中联系人信息
使用ListView显示手机中联系人的姓名和电话号码 父类布局activity_main.xml,子类布局line.xml(一个文件的单独存放) 运行截图: (避免泄露信息对部分地方进行了涂鸦O(∩_ ...
- HDU 5806 NanoApe Loves Sequence Ⅱ ——(尺取法)
题意:给出一个序列,问能找出多少个连续的子序列,使得这个子序列中第k大的数字不小于m. 分析:这个子序列中只要大于等于m的个数大于等于k个即可.那么,我们可以用尺取法写,代码不难写,但是有些小细节需要 ...
- DB 分库分表(5):一种支持自由规划无须数据迁移和修改路由代码的 Sharding 扩容方案
作为一种数据存储层面上的水平伸缩解决方案,数据库Sharding技术由来已久,很多海量数据系统在其发展演进的历程中都曾经历过分库分表的Sharding改造阶段.简单地说,Sharding就是将原来单一 ...
- 移动端隐藏scroll滚动条::-webkit-scrollbar
::-webkit-scrollbar {/*隐藏滚轮*/ display: none; } CSS3自定义滚动条样式 -webkit-scrollbar 前言 webkit支持拥有overflow属 ...
- linux系统创建windows启动盘
平时工作中用到linux的操作命令较多,因此为了方便,就给电脑装了双系统,一般工作的时候,都选择进入linux系统.但是今天有件工作之外的事情需要解决下:创建一个windows启动盘.如果按照往常来说 ...
- Log4j rootLogger根配置以及4种日志级别
Log4j 根配置语法 log4j.rootLogger = [ level ] , appenderName, appenderName, … 把指定级别以上的日志信息输出到指定的一个或者多个位置 ...
- 堆排序 java
<pre name="code" class="java">package heapSort; /** * 大根堆 * @author root * ...
- 淘宝TAE平台定时任务包的部署步骤
淘宝TAE平台定时任务包的部署: 第一步:首先把自己的任务打包成一个jar包.使用maven打包的命令为:mvn clean install 第二步:把任务jar包依赖的jar包全部导出来.使用mav ...