题目:
给定一个 24 小时制(小时:分钟 “HH:MM”)的时间列表,找出列表中任意两个时间的最小时间差并以分钟数表示。
解答:
def getMinutes(t: str) -> int:
#'0'->48,'9'->57
return ((ord(t[0]) - ord('0')) * 10 + ord(t[1]) - ord('0')) * 60 + (ord(t[3]) - ord('0')) * 10 + ord(t[4]) - ord('0')
class Solution:
def findMinDifference(self, timePoints: List[str]) -> int:
timePoints.sort()
ans = float('inf')
t0Minutes = getMinutes(timePoints[0])
preMinutes = t0Minutes
for i in range(1, len(timePoints)):
minutes = getMinutes(timePoints[i])
ans = min(ans, minutes - preMinutes) # 相邻时间的时间差
preMinutes = minutes
ans = min(ans, t0Minutes + 1440 - preMinutes) # 首尾时间的时间差
return ans