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 ...
随机推荐
- Java线程面试题 Top 50(转载)
原文链接:http://www.importnew.com/12773.html 本文由 ImportNew - 李 广 翻译自 javarevisited 不管你是新程序员还是老手,你一定在面试中遇 ...
- codeforces B. Multitasking 解题报告
题目链接:http://codeforces.com/problemset/problem/384/B 题目意思:给出n个数组,每个数组包括m个数字,当k = 0 时,需要把n个数组都按照从小到大的顺 ...
- TCP/IP 协议 —— ARP
通过 ARP 广播获得对方的 MAC 地址: 地址解析协议,即ARP(Address Resolution Protocol),是根据IP地址获取物理地址的一个TCP/IP协议. 1. 特点 ARP ...
- 「SDFZ听课笔记」二分图&&网络流
二分图? 不存在奇环(长度为奇数的环)的图 节点能黑白染色,使得不存在同色图相连的图 这两个定义是等价哒. 直观而言,就是这样的图: 二分图有一些神奇的性质,让一些在一般图上复杂度飞天的问题可以在正常 ...
- FTP:文件传输协议(指令及响应代码)
文件传输协议(FTP)使得主机间可以共享文件. FTP 使用 TCP 生成一个虚拟连接用于控制信息,然后再生成一个单独的 TCP 连接用于数据传输.控制连接使用类似 TELNET 协议在主机间交换命令 ...
- SQL连接查询和嵌套查询详解
连接查询 若一个查询同时涉及两个或两个以上的表,则称之为连接查询.连接查询是数据库中最最要的查询, 包括: 1.等值连接查询 2.自然连接查询 3.非等值连接查询 4.自身连接查询 5.外连接查询 6 ...
- ASoC框架
ASoC框架分为3部分: 1. platform(用来描述芯片的DAI接口,负责数据传输): DAI:snd_soc_dai_driver, 用来表示支持哪些格式数据, 提供设置格式的函数, 启动数据 ...
- mysql跨表更新示例
一.在同一个表中冗余存储记录之间的关系(组织机构树),查询时需要根据冗余字段进行关联查询 例如,下面的示例,用户表中有个字段friend标记其朋友关系,要求找出id=2及他的朋友(父节点) mysql ...
- seafile文档
https://manual-cn.seafile.com/deploy_windows/download_and_setup_seafile_windows_server.html
- python--numpy模块、spicy模块、 matplotlib模块
一:numpy模块 ndarray:存储单一数据类型的多维数组 ufunc:能够对数组进行处理的函数(universal function object) #numpy 中arange用法,指定开始值 ...