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 ...
随机推荐
- poj3180 The Cow Prom
The Cow Prom Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2373 Accepted: 1402 Desc ...
- [TJOI2009]开关 (线段树)
题目描述 现有N(2 ≤ N ≤ 100000)盏灯排成一排,从左到右依次编号为:1,2,......,N.然后依次执行M(1 ≤ M ≤ 100000)项操作,操作分为两种:第一种操作指定一个区间[ ...
- KS求有向图强连通分量模板
#include<bits/stdc++.h> using namespace std; typedef long long ll; int n,m; ; *maxn; struct no ...
- 简单的webservice发布和测试
eclipse发布参考:http://www.cnblogs.com/cherxu/p/5179053.html很简单,接口写好后,打个war包放到tomcat里面跑起来: 本例采用axis来测试:发 ...
- bzoj2144 跳跳棋 二分
[bzoj2144]跳跳棋 Description 跳跳棋是在一条数轴上进行的.棋子只能摆在整点上.每个点不能摆超过一个棋子.我们用跳跳棋来做一个简单的游戏:棋盘上有3颗棋子,分别在a,b,c这三个位 ...
- 洛谷 [P3205] 合唱队
区间DP 手动模拟一下,我们发现本题就是一个左右加数的区间DP #include <iostream> #include <cstdio> #include <cstri ...
- springboot开发 第一个案例之hello,world!
开发环境:(1)Eclipse Java EE Version: Neon Release (4.6.0) Build id: 20160613-1800 (2)apache-maven-3 ...
- 【HDOJ5955】Guessing the Dice Roll(概率DP,AC自动机,高斯消元)
题意: 有n个人,每个人有一个长为L的由1~6组成的数串,现在扔一个骰子,依次记录扔出的数字,如果当前扔出的最后L个数字与某个人的数串匹配,那么这个人就算获胜,现在问每个人获胜的概率是多少. n,l& ...
- ado:SqlDataAdapter的两种不同写法,以及SqlCommand的两种不同写法
原文发布时间为:2008-08-01 -- 来源于本人的百度文章 [由搬家工具导入] SqlDataAdapter:(它是自动打开连接且自动关闭的,所以可以不必显示打开关闭连接) SqlConnect ...
- php——离线执行任务
<?php//设置忽略是否关闭终端窗口ignore_user_abort(true);ini_set('max_execution_time', '0');//采集页面函数,看不懂执行百度cur ...