给定一个 24 小时制(小时:分钟 “HH:MM”)的时间列表,找出列表中任意两个时间的最小时间差并以分钟数表示。
示例 1:
输入:timePoints = [“23:59”,“00:00”]
输出:1
思路:
根据时间进行排序
然后依次计算时间差(最后需要重新计算一下第一与最后一个的时间差,例如0:0与23:59时间差为1)
代码:
class Solution { public: int func(string s){ return
((s[0]-'0')*10+(s[1]-'0'))*60+((s[3]-'0')*10+(s[4]-'0')); } int
findMinDifference(vector<string>& timePoints) {
sort(timePoints.begin(),timePoints.end()); int res=INT_MAX; for(int
i=0;i<timePoints.size()-1;i++){
res=min(res,func(timePoints[i+1])-func(timePoints[i])); }
res=min(res,1440+(func(timePoints[0])-func(timePoints[timePoints.size()-1])));
return res; } };