题目
给定一个 24 小时制(小时:分钟 “HH:MM”)的时间列表,找出列表中任意两个时间的最小时间差并以分钟数表示。
示例 1:
输入:timePoints = ["23:59","00:00"] 输出:1
示例 2:
输入:timePoints = ["00:00","23:59","00:00"] 输出:0
解题
方法一:排序
class Solution { private: int getMinutes(string& t){ return ((t[0]-'0')*10+t[1]-'0')*60+(t[3]-'0')*10+t[4]-'0'; } public: int findMinDifference(vector<string>& timePoints) { sort(timePoints.begin(),timePoints.end()); int res=INT_MAX; int t0Minutes=getMinutes(timePoints[0]); int preMinutes=t0Minutes; for(int i=1;i<timePoints.size();i++){ int minutes=getMinutes(timePoints[i]); res=min(res,minutes-preMinutes); preMinutes=minutes; } res=min(res,t0Minutes+1440-preMinutes); return res; } };