class Solution {
public:
int findMinDifference(vector<string>& timePoints) {
int siz = timePoints.size();
sort(timePoints.begin(), timePoints.end());//一定要学会这个函数
int min_diff = 24*60;
for(int i = 0; i < siz; ++i){
int result = time_difference(timePoints[i], timePoints[(i+1)%siz]);//超过siz从头开始
if(result < min_diff){
min_diff = result;
}
}
return min_diff;
}
int time_to_minute(string s){
return 60*stoi(s.substr(0,2)) + stoi(s.substr(3,2));
}
int time_difference(string s1, string s2){
int diff = abs(time_to_minute(s2)-time_to_minute(s1));//字面上最小差距,绝对值的意义在于最后一个和第一个相减
return min(diff, 24*60-diff);//和另外半周相比,取最小值
}
};
(i+1)%N与(i-1+N)%N异曲同工