[Leetcode Week10]Minimum Time Difference
Minimum Time Difference 题解
原创文章,拒绝转载
题目来源:https://leetcode.com/problems/minimum-time-difference/description/
Description
Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minutes difference between any two time points in the list.
Example 1:
Input: ["23:59","00:00"]
Output: 1
Note:
- The number of time points in the given list is at least 2 and won't exceed 20000.
- The input time is legal and ranges from 00:00 to 23:59.
Solution
class Solution {
public:
void timeConvert(vector<int>& times, vector<string>& timePoints) {
int size = timePoints.size();
for (int i = 0; i < size; i++) {
times[i] = ((timePoints[i][0] - '0') * 10 +
(timePoints[i][1] - '0')) * 60 +
(timePoints[i][3] - '0') * 10 +
(timePoints[i][4] - '0');
}
}
int getDiff(int a, int b) {
int t = b - a;
if (t > 720)
return 1440 - t;
else
return t;
}
int findMinDifference(vector<string>& timePoints) {
if (timePoints.empty() || timePoints.size() == 1)
return 0;
int size = timePoints.size();
vector<int> times(size);
timeConvert(times, timePoints);
sort(times.begin(), times.end());
int mind = getDiff(times[0], times[1]);
int t;
for (int i = 0; i <= size - 2; i++) {
t = getDiff(times[i], times[i + 1]);
if (t < mind)
mind = t;
}
t = getDiff(times[0], times[size - 1]);
if (t < mind)
mind = t;
return mind;
}
};
解题描述
这道题考察的主要是字符串的比较处理,相对简单,主要问题在于时间字符串与时间数值之间的转换。
[Leetcode Week10]Minimum Time Difference的更多相关文章
- LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- 51. leetcode 530. Minimum Absolute Difference in BST
530. Minimum Absolute Difference in BST Given a binary search tree with non-negative values, find th ...
- [LeetCode] Minimum Time Difference 最短时间差
Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minut ...
- [LeetCode] Minimum Absolute Difference in BST 二叉搜索树的最小绝对差
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- LeetCode Minimum Time Difference
原题链接在这里:https://leetcode.com/problems/minimum-time-difference/description/ 题目: Given a list of 24-ho ...
- LeetCode Minimum Absolute Difference in BST
原题链接在这里:https://leetcode.com/problems/minimum-absolute-difference-in-bst/#/description 题目: Given a b ...
- 【LeetCode】1200. Minimum Absolute Difference 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcode ...
- 【LeetCode】530. Minimum Absolute Difference in BST 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【LeetCode】539. Minimum Time Difference 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/minimum-t ...
随机推荐
- kubernetes(k8s) 集群
开启和重启命令: sudo systemctl stop etcdsudo systemctl stop dockersudo systemctl stop kube-apiserversudo sy ...
- 简历编写技巧-java开发工程师简历实战
看到一遍简历编写的文章 想到也快找工作了 早晚能够用上 现在摘录如下 640?wx_fmt=jpeg 工欲善其事,必先利其器,这是自古以来的道理.所以如果想找到一份好的工作,一定要先整理一份好的简历. ...
- centos tomcat开机自启
在 /etc/rc.local 下 输入tomcat bin目录下的startup.sh /usr/tomcat8/bin/startup.sh 即可
- 微信公众号开发java框架:wx4j(MaterialUtils篇)
wx4j-MaterialUtils的使用 函数说明:上传永久视频素材 参数:文件路径.视频描述(通过setter填充内容即可) 返回值:微信服务器返回的json字符串 public static S ...
- 文件系统之 stat与access
stat命令 stat既有命令也有同名函数,用来获取文件Inode里主要信息,所以stat命令的输出信息比ls命令的输出信息要更详细,stat 跟踪符号链接,lstat不跟踪符号链接,其中会输出对应文 ...
- Java作业09-异常
6. 为如下代码加上异常处理 byte[] content = null; FileInputStream fis = new FileInputStream("testfis.txt&qu ...
- 下拉框select chosen被遮盖
最简单的就是让容器高度大点. 用js调整也行. 为什么z-index不管事,看下面... 浏览器支持 所有主流浏览器都支持 z-index 属性. 注释:任何的版本的 Internet Explore ...
- 【版本控制】VisualSVN Server更改SVN版本库存放路径的方法
最近也玩起了SVN软件版本管理,在本机上安装了VisualSVN Server+TortoiseSVN,感觉还不错吧.但是,版本库存在哪里呢?在安装VisualSVN Server时,已经默认设置了, ...
- request对数据进行编码的 才是导致乱码问题之一
1.此时服务器端接收到客户端提交来的post请求 2.request.getParameter("name")方法开始从请求中解析数据 并使用默认的编码 格式进行编码(ISO-88 ...
- BZOJ 2502: 清理雪道 | 有上下界最小流
#include<cstdio> #include<algorithm> #include<cstring> #include<queue> #defi ...