539 Minimum Time Difference 最小時間差
阿新 • • 發佈:2018-04-23
tor dmi end 時間 minimum grand gin light fin
給定一個 24 小時制(小時:分鐘)的時間列表,找出列表中任意兩個時間的最小時間差並已分鐘數表示。
示例 1:
輸入: ["23:59","00:00"]
輸出: 1
備註:
1.列表中時間數在 2~20000 之間。
2.每個時間取值在 00:00~23:59 之間。
詳見:https://leetcode.com/problems/minimum-time-difference/description/
C++:
class Solution { public: int findMinDifference(vector<string>& timePoints) { int res = INT_MAX, n = timePoints.size(), diff = 0; sort(timePoints.begin(), timePoints.end()); for (int i = 0; i < n; ++i) { string t1 = timePoints[i], t2 = timePoints[(i + 1) % n]; int h1 = (t1[0] - ‘0‘) * 10 + t1[1] - ‘0‘; int m1 = (t1[3] - ‘0‘) * 10 + t1[4] - ‘0‘; int h2 = (t2[0] - ‘0‘) * 10 + t2[1] - ‘0‘; int m2 = (t2[3] - ‘0‘) * 10 + t2[4] - ‘0‘; diff = (h2 - h1) * 60 + (m2 - m1); if (i == n - 1) { diff += 24 * 60; } res = min(res, diff); } return res; } };
參考:http://www.cnblogs.com/grandyang/p/6568398.html
539 Minimum Time Difference 最小時間差