首页 > 其他 > 详细

539. Minimum Time Difference

时间:2020-04-15 13:25:03      阅读:62      评论:0      收藏:0      [点我收藏+]

Problem:

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:

Input: ["23:59","00:00"]
Output: 1

Note:

  1. The number of time points in the given list is at least 2 and won‘t exceed 20000.
  2. The input time is legal and ranges from 00:00 to 23:59.

思路

Solution (C++):

int findMinDifference(vector<string>& timePoints) {
    int n = timePoints.size();
    if (n < 2)  return 0;
    int min_diff = 24*60;
    
    vector<int> time(n, 0);
    for (int i = 0; i < n; ++i) {
        time[i] = stoi(timePoints[i].substr(0,2)) * 60 + stoi(timePoints[i].substr(3,2)); 
    }
    
    sort(time.begin(), time.end());   
    time.push_back(time[0] + min_diff);
    for (int i = 0; i < n; ++i) {
        min_diff = min(min_diff, time[i+1]-time[i]);
    }
    return min_diff;
}

性能

Runtime: 28 ms??Memory Usage: 9.7 MB

思路

Solution (C++):


性能

Runtime: ms??Memory Usage: MB

539. Minimum Time Difference

原文:https://www.cnblogs.com/dysjtu1995/p/12704088.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!