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 ...
随机推荐
- vue模板语法下集
1. 样式绑定 1.1 class绑定 使用方式:v-bind:class="expression" expression的类型:字符串.数组.对象 1.2 style绑定 v-b ...
- ACM之一个简单的数学问题
一个简单的数学题 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 zyc最近迷上了数学,一天,dj想出了一道数学题来难住他.算出1/n,但zyc一时答不上来希望大家能编程 ...
- HDU4135 Co-prime
题目链接:Click here Solution: 简单容斥,我们先把\(N\)分解质因数,我们知道\(1\sim x\)里能整除\(i\)的数的个数为\(\lfloor \frac{x}{i} \r ...
- apt 和 apt-get的区别
apt 和 apt-get的区别 - liudsl的博客 - CSDN博客 https://blog.csdn.net/liudsl/article/details/79200134 Linux软件 ...
- BZOJ刷题列表【转载于hzwer】
沿着黄学长的步伐~~ 红色为已刷,黑色为未刷,看我多久能搞完吧... Update on 7.26 :之前咕了好久...(足见博主的flag是多么emmm......)这几天开始会抽时间刷的,每天几道 ...
- 按下home键,重新打开,应用重启
其实不是重启,只是重新打开了luncher的那个activity.只要通过判断把它finish,就会显示按下home键前的页面. 解决方法: 在重启的页面中加入一下代码,注意加在setContentV ...
- ThreadPoolExecutor 优雅关闭线程池的原理.md
经典关闭线程池代码 ExecutorService executorService = Executors.newFixedThreadPool(10); executorService.shutdo ...
- Linux 命令速记本
# 比较1.txt和2.txt的差异 comm [---] .txt .txt # 求1.txt和2.txt的MD5用于区分两个文件是否相同 md5sum .txt .txt #tr 用于转换或删除文 ...
- vue echarts圆角阴影效果
series: [ { name: '销量', type: 'bar', data: [5, 20, 36, 10, 10, 20], itemStyle: { normal: { barBorder ...
- CL_GUI_FRONTEND_SERVICES 使用问题
CL_GUI_FRONTEND_SERVICES(SAP操作Windows文件) 这个类下面的方法均为静态方法,引用的时候以=>来引用方法 注意:在执行CL_GUI_FRONTEND_SERVI ...