题目地址:https://leetcode.com/problems/minimum-time-difference/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:
1、 Thenumberoftimepointsinthegivenlistisatleast2andwon'texceed20000.;
2、 Theinputtimeislegalandrangesfrom00:00to23:59.;
题目大意
给出了一个时间数组,找出这个数组中最接近的两个时间的差。
解题方法
容易想到时间是个循环,正如题目中的所示,需要考虑循环问题。所以解决的方案是先求出每个时间点超出0点的分钟数,对时间进行排序。然后采取zip循环的方式,找出每两个时间之间的时间差,求最小值即可。
注意对24小时的分钟总数进行了求余,这样能保证题目中所示的例子能得到正确结果。
补充一下zip的用法:
>>> a = [1,2,3]
>>> b = [4,5,6]
>>> c = [4,5,6,7,8]
>>> zipped = zip(a,b) 打包为元组的列表
[(1, 4), (2, 5), (3, 6)]
>>> zip(a,c) 元素个数与最短的列表一致
[(1, 4), (2, 5), (3, 6)]
>>> zip(*zipped) 与 zip 相反,可理解为解压,返回二维矩阵式
[(1, 2, 3), (4, 5, 6)]
1 2 3 4 5 6 7 8 9
代码如下:
class Solution(object):
def findMinDifference(self, timePoints):
"""
:type timePoints: List[str]
:rtype: int
"""
def convert(time):
return int(time[:2]) * 60 + int(time[3:])
timePoints = map(convert, timePoints)
timePoints.sort()
return min((y - x) % (24 * 60) for x, y in zip(timePoints, timePoints[1:] + timePoints[:1]))
1 2 3 4 5 6 7 8 9 10 11
DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有
本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发