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 ...
随机推荐
- 使用HttpClient实现对第三方服务器的请求并接受返回数据
/* * 创建日期 2017-4-7 * * TODO 要更改此生成的文件的模板,请转至 * 窗口 - 首选项 - Java - 代码样式 - 代码模板 */ package com.enfo.int ...
- LightOJ1106 Gone Fishing
Gone Fishing John is going on a fishing trip. He has h hours available, and there are n lakes in the ...
- 【Tomcat】使用tomcat manager 管理和部署项目,本地部署项目到服务器
在部署tomcat项目的时候,除了把war文件直接拷贝到tomcat的webapp目录下,还有一种方法可以浏览器中管理和部署项目,那就是使用tomcat manager. 默认情况下,tomcat m ...
- (48)C#网络4 web
WebClient 类 提供用于将数据发送到和接收来自通过 URI 确认的资源数据的常用方法 private delegate string delegatehWeb(); private void ...
- 洛谷——P2733 家的范围 Home on the Range
P2733 家的范围 Home on the Range 题目背景 农民约翰在一片边长是N (2 <= N <= 250)英里的正方形牧场上放牧他的奶牛.(因为一些原因,他的奶牛只在正方形 ...
- springboot主要注解及其作用
1.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...
- Hadoop HDFS 常用命名
HDFS命令基本格式:hadoop fs -cmd < args > ls 命令hadoop fs -ls / 列出hdfs文件系统根目录下的目录和文件 hadoop fs -ls -R ...
- spark学习(二)
Spark是一个通用的并行计算框架,由UCBerkeley的AMP实验室开发. Spark和Hadoop有什么不同呢? Spark是基于map reduce算法实现的分布式计算,拥有Hadoop Ma ...
- 23. 客户默认选项(Default Customer Options)
Editing Email Templates Email Sender Contact Us
- C#如何实现挂机锁
首先在主窗体中设置一个子窗体的实例,然后当点击挂机之后,隐藏当前窗体,同时显示子窗体. 把子窗体的背景窗体设置如下属性(主要是背景随便改成一个图片,然后FormBorderStyle改成None, ...