Given a 24 Hour system ( hour : minute “HH:MM”) Time list of , Find the minimum time difference between any two times in the list and express it in minutes .
Examples 1:
input :timePoints = [“23:59”,“00:00”]
output :1
thinking :
Sort by time
Then calculate the time difference in turn ( Finally, we need to recalculate the time difference between the first and the last , for example 0:0 And 23:59 The time difference is 1)
code :
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; } };
Technology