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 ...
随机推荐
- 用bootstrap写一个登陆页
<div class="container"> <p class="text-center marginTop35">后台管理系统< ...
- vue整合adminLTE
前端框架AdminLTE 中文教程 如何用vue整合adminlte模板 1.adminlte 下载地址 : https://github.com/almasaeed2010/AdminLTE/rel ...
- js判断是否是app,及版本号
判断是否是android,ios,qq,wetchat export const Config = {}; Config.ua = navigator.userAgent.toLowerCase(); ...
- 修复Long类型太长转为JSON格式的时候出错的问题
这边项目要求ID是自动生成的20位Long型数字 但是实际中应用的时候回发生一种问题就是,查询的时候debug的时候数据都正常,但是返回前端的时候就会发现,数据错误了. 大体就是类似于下面的这种情况. ...
- MediaFoundation初步研究
用MediaFoundation写了个媒体播放程序,封装成了DLL,MFVideo.dll,写了个使用例子
- Fastadmin 后台表单,外键关键,步骤
1.在 public\assets\js\backend 目录中找到对应的js,修改 2.对应控制器中加上index()方法:开启关联查询 重点: protected $relationSearch ...
- 在Linux上部署Nginx,反向代理tornado的WebSite
1.安装 Nginx yum install -y nginx 2. 修改nginx配置文件 cd /etc/nginx/ mv nginx.conf nginx.conf.swf mv nginx. ...
- 凸包Graham Scan算法实现
凸包算法实现点集合中搜索凸包顶点的功能,可以处理共线情况,可以输出共线点也可以不输出而只输出凸包顶点.经典的Graham Scan算法,点排序使用极角排序方式,并对共线情况做特殊处理.一般算法是将共线 ...
- ORA-02291: 违反完整约束条件 - 未找到父项关键字问题解决
ORA-02291: 违反完整约束条件 - 未找到父项关键字问题解决 总体说说可能出现的原因: 情况场景: 表A中有个字段是外键,关联了表B中的某字段,再往表A插入数据时,会出现这种情况. 可能原因: ...
- idea用法
更新gradle的依赖后,刷新项目引入jar包的方法: view--Tool Buttons 在右侧 Gradle 点刷新