码迷,mamicode.com
首页 > 其他好文 > 详细

539 Minimum Time Difference 最小时间差

时间:2018-04-23 00:11:34      阅读:539      评论:0      收藏:0      [点我收藏+]

标签:epo   begin   nim   取值   +=   find   vector   time   rip   

给定一个 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 最小时间差

标签:epo   begin   nim   取值   +=   find   vector   time   rip   

原文地址:https://www.cnblogs.com/xidian2014/p/8910267.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!