Leetcode 539. 最小时间差 (直接做O(n^2), 贪心排序优化到O(nlogn)

Leetcode 539. 最小时间差 (直接做O(n^2), 贪心排序优化到O(nlogn)

 

class Solution {
public:
    int findMinDifference(vector<string>& timePoints) {
        vector<int> minutes;
        for (auto timePoint: timePoints) {
            int hour = stoi(timePoint.substr(0, 2));
            int minute = stoi(timePoint.substr(3));
            minutes.push_back(hour * 60 + minute);
        }
        sort(minutes.begin(), minutes.end());
        minutes.push_back(minutes[0] + 24 * 60);
        int res = INT_MAX;
        for (int i = 1; i < minutes.size(); i++) {
            int t = minutes[i] - minutes[i - 1];
            res = min(res, t);
        }
        return res;
    }
};

上一篇:MDK Keil编译程序出现..\source\main.c(118): warning: #69-D: integer conversion resulted in truncation问题


下一篇:类型转换