马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目描述:给定一个 24 小时制(小时:分钟 "HH:MM")的时间列表,找出列表中任意两个时间的最小时间差并以分钟数表示。
示例 1:
输入:timePoints = ["23:59","00:00"]
输出:1
示例 2:
输入:timePoints = ["00:00","23:59","00:00"]
输出:0
提示:
2 <= timePoints <= 2 * 104
timePoints[i] 格式为 "HH:MM"
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/minimum-time-difference
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution {
public:
int findMinDifference(vector<string>& timePoints) {
//转化成分钟
vector<int>store;
int len = timePoints.size();
for(int i = 0; i < len; i++){
string temp = timePoints[i];
int temp1 = stoi(temp.substr(0, 2))*60 + stoi(temp.substr(3, 2));
store.push_back(temp1);
}
sort(store.begin(), store.end());
int len1 = store.size();
int res = min(store[len1-1] - store[0], store[0]+ 24*60- store[len1 - 1]);//注意这一点!!!
for(int i = 1 ; i < len1; i++){
res = min(store[i] - store[i-1], res);
}
return res;
}
};
|