[leetcode]539. Minimum Time Difference

162 篇文章 0 订阅

[leetcode]539. Minimum Time Difference


Analysis

今天要吃柚子—— [每天刷题并不难0.0]

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.
先对输入的字符串数组排序,然后遍历一下算差值,最后再取最小的差值就可以了。

Implement

class Solution {
public:
    int findMinDifference(vector<string>& timePoints) {
        int res = INT_MAX;
        int len = timePoints.size();
        sort(timePoints.begin(), timePoints.end());
        int h1, h2, m1, m2;
        int diff;
        string time1, time2;
        for(int i=0; i<len; i++){
            time1 = timePoints[i];
            time2 = timePoints[(i+1)%len];
            h1 = (time1[0]-'0')*10+(time1[1]-'0');
            m1 = (time1[3]-'0')*10+(time1[4]-'0');
            h2 = (time2[0]-'0')*10+(time2[1]-'0');
            m2 = (time2[3]-'0')*10+(time2[4]-'0');
            diff = (h2-h1)*60+(m2-m1);
            if(i == len-1)
                diff += 24*60;
            res = min(res, diff);
        }
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值