539 Minimum Time Difference 最小时间差
给定一个 24 小时制(小时:分钟)的时间列表,找出列表中任意两个时间的最小时间差并已分钟数表示。
示例 1:
输入: ["23:59","00:00"]
输出: 1
备注:
1.列表中时间数在 2~20000 之间。
2.每个时间取值在 00:00~23:59 之间。
详见:https://leetcode.com/problems/minimum-time-difference/description/
C++:
class Solution {
public:
int findMinDifference(vector<string>& timePoints)
{
int res = INT_MAX, n = timePoints.size(), diff = 0;
sort(timePoints.begin(), timePoints.end());
for (int i = 0; i < n; ++i)
{
string t1 = timePoints[i], t2 = timePoints[(i + 1) % n];
int h1 = (t1[0] - '0') * 10 + t1[1] - '0';
int m1 = (t1[3] - '0') * 10 + t1[4] - '0';
int h2 = (t2[0] - '0') * 10 + t2[1] - '0';
int m2 = (t2[3] - '0') * 10 + t2[4] - '0';
diff = (h2 - h1) * 60 + (m2 - m1);
if (i == n - 1)
{
diff += 24 * 60;
}
res = min(res, diff);
}
return res;
}
};
参考:http://www.cnblogs.com/grandyang/p/6568398.html
539 Minimum Time Difference 最小时间差的更多相关文章
- 539. Minimum Time Difference
Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minut ...
- LC 539. Minimum Time Difference
Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minut ...
- 【LeetCode】539. Minimum Time Difference 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/minimum-t ...
- [LeetCode] Minimum Time Difference 最短时间差
Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minut ...
- Python解Leetcode: 539. Minimum Time Difference
题目描述:给定一个由时间字符组成的列表,找出任意两个时间之间最小的差值. 思路: 把给定的链表排序,并且在排序的同时把60进制的时间转化成十进制整数: 遍历排序的数组,求出两个相邻值之间的差值: 求出 ...
- [Swift]LeetCode539. 最小时间差 | Minimum Time Difference
Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minut ...
- LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- [LeetCode] Minimum Absolute Difference in BST 二叉搜索树的最小绝对差
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- [Swift]LeetCode530. 二叉搜索树的最小绝对差 | Minimum Absolute Difference in BST
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
随机推荐
- [APIO 2014] 序列分割
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3675 [算法] 首先 , 我们发现将一段序列切成若干段所获得的收益与顺序无关 于是我 ...
- MongoDB安装及多实例启动
MongoDB安装及多实例启动 MongoDB简介 MongoDB是一款跨平台.面向文档的数据库.可以实现高性能.高可用性,并且能够轻松扩展,是一个基于分布式文件存储的开源数据库系统,在高负载的情况下 ...
- python虚拟环境管理包virtualenvwrapper
1.打开cmd 2.安装virtualenvwrapper pip install virtualenvwrapper-win 3.配置虚拟环境的位置 新建系统变量默认在c盘 4.新建虚拟环境 mkv ...
- hibernate的优缺点和适用场合
Hibernate优点: 1.对象化.人员以面相对象的思想来操作数据库.Hibernate支持许多面向对象的特性,如组合,继承,多态等. 2.更好的移植性.对于不同的数据库,开发者只需要使用相同的数据 ...
- ununtu 下安装 Nvidia 显卡驱动
本人电脑硬件配置:CPU : AMD Athlon(tm) II X2 215 Processor × 2 显示卡 : GeForce 6150SE nForce 430/integrated/SSE ...
- tetrahedron
题意: 求解一个四面体的内切球. 解法: 首先假设内切球球心为$(x0,x1,x2)$,可以用$r = \frac{3V}{S_1+S_2+S_3+S_4}$得出半径, 这样对于四个平面列出三个方程, ...
- Hadoop2 使用 YARN 运行 MapReduce 的过程源码分析
Hadoop 使用 YARN 运行 MapReduce 的过程如下图所示: 总共分为11步. 这里以 WordCount 为例, 我们在客户端终端提交作业: # 把本地的 /home/hadoop/t ...
- 2.1-2.2 Hive 中数据库(Table、Database)基本操作
官网文档:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL 一.create table 1.官方字段 # # C ...
- mosquitto.conf之log配置
# ================================================================= # Logging # 日志信息 # ============= ...
- 【Hadoop】MapReduce笔记(二):MapReduce容错,任务失败处理
典型问题:Hadoop如何判断一个任务失败?失败了怎么做? 分析:实际情况下,用户代码存在软件错误.进程崩溃.机器故障等都会导致失败.Hadoop判断的失败有不同级别类型,针对不同级别的失败有不同的处 ...