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

  1. [LeetCode] 681. Next Closest Time 下一个最近时间点

    Given a time represented in the format "HH:MM", form the next closest time by reusing the ...

  2. LeetCode 681. Next Closest Time 最近时刻 / LintCode 862. 下一个最近的时间 (C++/Java)

    题目: 给定一个"HH:MM"格式的时间,重复使用这些数字,返回下一个最近的时间.每个数字可以被重复使用任意次. 保证输入的时间都是有效的.例如,"01:34" ...

  3. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

  4. LeetCode (13): 3Sum Closest

    https://leetcode.com/problems/3sum-closest/ [描述] Given an array S of n integers, find three integers ...

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

  7. [LeetCode] Find the Closest Palindrome 寻找最近的回文串

    Given an integer n, find the closest integer (not including itself), which is a palindrome. The 'clo ...

  8. LeetCode 973. K Closest Points to Origin

    原题链接在这里:https://leetcode.com/problems/k-closest-points-to-origin/ 题目: We have a list of points on th ...

  9. [LeetCode] 16. 3Sum Closest 最近三数之和

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

随机推荐

  1. BZOJ3532 [Sdoi2014]Lis 【网络流退流】

    题目 给定序列A,序列中的每一项Ai有删除代价Bi和附加属性Ci.请删除若 干项,使得4的最长上升子序列长度减少至少1,且付出的代价之和最小,并输出方案. 如果有多种方案,请输出将删去项的附加属性排序 ...

  2. es6 箭头函数 map、find

    var  value = arr.map(function (x) {return x * x}); const arr = [1,2,3,4]; const value = arr.map(x =& ...

  3. Linux 下测试磁盘读写 I/O 速度的方法汇总

    在分布式异构存储系统中,我们经常会需要测量获取不同节点中硬盘/磁盘的读写 I/O 速度,下面是 Linux 系统下一些常用测试方法(之后不定期更新): 1.使用 hdparm 命令这是一个是用来获取A ...

  4. OTOCI(bzoj 1180)

    Description 给出n个结点以及每个点初始时对应的权值wi.起始时点与点之间没有连边.有3类操作: 1.bridge A B:询问结点A与结点B是否连通.如果是则输出“no”.否则输出“yes ...

  5. POJ 1182 食物链 [并查集 带权并查集 开拓思路]

    传送门 P - 食物链 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit  ...

  6. Laravel 之Artisan

    简介: Artisan是Laravel中自带的命令行工具的名称: 由强大的Symfony Console组件驱动的: 提供了一些对应用开发有帮助的命令: 查看所有可用的Artisan的命令 php a ...

  7. File类 递归 获取目录下所有文件文件夹

    package com.xiwi; import java.io.*; import java.util.*; class file{ public static void main(String a ...

  8. Java常用API(Scanner,Random)匿名对象

    API:即Application programming Interface,应用编程接口. Java中封装了许许多多的API供用户使用,Scanner与Random便是其中之一,API实际就是类,已 ...

  9. java中的数据转换与前置,后置加加

    public class Demo{ public static void main(String [] args){ int num=2; System.out.println(num++);//后 ...

  10. Linux出现cannot create temp file for here-document: No space left on device的问题解决

    在终端输入:cd /ho 按tab键时,显示错误: bash: cannot create temp file for here-document: No space left on device 这 ...