leetcode 681. 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 tim
题目大意:
用当前时间的每一位数组成新的时间,使得新时间和原来的时间差值最小。输出这个新时间
思路:
由于时间的位数很小,可以枚举每一位,组合成新的时间,并把新时间和原始时间装换成秒,进行比较。注意时间的格式,如不可能出现25:91这种时间之类的
class Solution {
public:
int get(string& s, int i, int j, int k, int p) {
int x = (s[i] - ')') * 10 + s[j] - '0';
int y = (s[k] - '0') * 10 + s[p] - '0';
int sec = x * 3600 + y * 60;
return sec;
}
string nextClosestTime(string time) {
string s = "";
for (int i = 0; i < 5; ++i) if(time[i] != ':') s += time[i];
int be = get(s, 0, 1, 2, 3);
int ans = 100000000;
string t = "";
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
for (int k = 0; k < 4; ++k) {
for (int p = 0; p < 4; ++p) {
//if (i == 0 && j == 1 && k == 2 && p == 3) continue;
if (s[k] >= '6') continue;
if (s[i] >= '3') continue;
if (s[i] == '2' && s[j] > '4') continue;
if (s[i] == '2' && s[j] == '4') {
if (s[k] != '0' || s[p] != '0') continue;
}
int as = get(s, i, j, k, p);
if (as == be) continue;
if (as < be) {
as += 24 * 3600;
}
if (as < ans) {
ans = as;
t = "";
t += s[i];
t += s[j];
t += s[k];
t += s[p];
}
}
}
}
}
string w = "";
for (int i = 0; i < t.size(); ++i) {
w += t[i];
if (i == 1) w += ':';
}
if (w == "") {
return time;
}
return w;
}
};
代码写的有点长,不够思路简单啊
leetcode 681. Next Closest Time的更多相关文章
- [LeetCode] 681. Next Closest Time 下一个最近时间点
Given a time represented in the format "HH:MM", form the next closest time by reusing the ...
- LeetCode 681. Next Closest Time 最近时刻 / LintCode 862. 下一个最近的时间 (C++/Java)
题目: 给定一个"HH:MM"格式的时间,重复使用这些数字,返回下一个最近的时间.每个数字可以被重复使用任意次. 保证输入的时间都是有效的.例如,"01:34" ...
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- LeetCode (13): 3Sum Closest
https://leetcode.com/problems/3sum-closest/ [描述] Given an array S of n integers, find three integers ...
- 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] Find K Closest Elements 寻找K个最近元素
Given a sorted array, two integers k and x, find the k closest elements to x in the array. The resul ...
- [LeetCode] Find the Closest Palindrome 寻找最近的回文串
Given an integer n, find the closest integer (not including itself), which is a palindrome. The 'clo ...
- LeetCode 973. K Closest Points to Origin
原题链接在这里:https://leetcode.com/problems/k-closest-points-to-origin/ 题目: We have a list of points on th ...
- [LeetCode] 16. 3Sum Closest 最近三数之和
Given an array nums of n integers and an integer target, find three integers in nums such that the s ...
随机推荐
- 算法复习——计算几何基础(zoj1081)
题目: Statement of the Problem Several drawing applications allow us to draw polygons and almost all o ...
- 【2018.12.10】NOI模拟赛3
题目 WZJ题解 大概就是全场就我写不过 $FFT$ 系列吧……自闭 T1 奶一口,下次再写不出这种 $NTT$ 裸题题目我就艹了自己 -_-||| 而且这跟我口胡的自创模拟题 $set1$ 的 $T ...
- lua学习随笔
1.1 Chunks 1.2 全局变量 访问一个没有初始化的全局变量也不会出错,只不过的到的结果是nil 如果想删除一个全局变量,只需要将变量赋值为nil 1.3 词法约定 标识符 保留字不能作为 ...
- 修路 BZOJ 4774
修路 [问题描述] 村子间的小路年久失修,为了保障村子之间的往来,法珞决定带领大家修路.对于边带权的无向图 G = (V, E),请选择一些边,使得1 <= i <= d, i号节点和 n ...
- GridView选中,编辑,取消,删除代码
原文发布时间为:2008-08-03 -- 来源于本人的百度文章 [由搬家工具导入] 2.GridView选中,编辑,取消,删除: 效果图: 后台代码:你可以使用sqlhelper,本文没用。代码如下 ...
- C# 用this修饰符为原始类型扩展方法
特点:1.静态类 2.静态方法 3.第一个参数前加this 例如:public static List<T> ToList<T>(this string Json),就是为th ...
- BZOJ——2096: [Poi2010]Pilots
http://www.lydsy.com/JudgeOnline/problem.php?id=2096 Time Limit: 30 Sec Memory Limit: 162 MBSubmit: ...
- unix grep命令的大致实现
用到了strstr(a,b)函数和getline()函数,strstr(a,b)函数看是否能在字符串a中找到字符串b,若找到返回指向,若没找到返回NULL strstr实现可以看:Implement ...
- 新建mvc项目
第一步 第二步 第三步,ok项目建好
- excel转换html
利用POI解析excel,转换成html,支持各种版本的excel.支持自定义样式.支持行列合并 需要用到的jar public class Excel2Html { /** * 读取Excel并转换 ...