Given a list of 24-hour clock time points in “Hour:Minutes” format, find the minimum minutes difference between any two time points in the list. Example 1:
class { public: intfindMinDifference(vector<string>& timePoints){ vector<bool> timeSlot(24*60, false); for(string s : timePoints){ stringstreamss(s); string part; vector<string> tp; while(getline(ss, part, ':')){ tp.push_back(part); } int hour = stoi(tp[0]); int mini = stoi(tp[1]); if(timeSlot[hour*60+mini]) return0; timeSlot[hour*60+mini] = true; } int first = INT_MAX, last = INT_MIN; int res = INT_MAX, pre = 0; for(int i = 0; i < timeSlot.size(); i++){ if(timeSlot[i]){ if(first != INT_MAX){ res = min(res, i - pre); } first = min(first, i); last = max(last, i); pre = i; } } return min(res, 24*60-last+first); } };
近期评论