# [LeetCode] Minimum Time Difference 最短时间差

+关注继续查看

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.

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;
}
};

public:
int findMinDifference(vector<string>& timePoints) {
int res = INT_MAX, n = timePoints.size();
vector<int> nums;
for (string str : timePoints) {
int h = stoi(str.substr(0, 2)), m = stoi(str.substr(3));
nums.push_back(h * 60 + m);
}
sort(nums.begin(), nums.end());
for (int i = 1; i < n; ++i) {
res = min(res, nums[i] - nums[i - 1]);
}
return min(res, 1440 + nums[0] - nums.back());
}
};

public:
int findMinDifference(vector<string>& timePoints) {
int res = INT_MAX, pre = 0, first = INT_MAX, last = INT_MIN;
for (string str : timePoints) {
int h = stoi(str.substr(0, 2)), m = stoi(str.substr(3));
if (mask[h * 60 + m] == 1) return 0;
mask[h * 60 + m] = 1;
}
for (int i = 0; i < 1440; ++i) {
if (first != INT_MAX) {
res = min(res, i - pre);
}
first = min(first, i);
last = max(last, i);
pre = i;
}
}
return min(res, 1440 + first - last);
}
};

https://discuss.leetcode.com/topic/83250/easy-c-solution

https://discuss.leetcode.com/topic/82573/verbose-java-solution-bucket

https://discuss.leetcode.com/topic/82575/java-o-nlog-n-o-n-time-o-1-space-solutions

，如需转载请自行联系原博主。

LeetCode 380: 常数时间插入、删除和获取随机元素 Insert Delete GetRandom O(1)

693 0
LeetCode 122 Best Time to Buy and Sell Stock II（股票买入卖出的最佳时间 II）

654 0
jQuery EasyUI API 中文文档 - 日期时间框（DateTimeBox）
DateTimeBox  日期时间框 扩展自 $.fn.datebox.defaults。用$.fn.datetimebox.defaults 重写了 defaults。     依赖 datebox timespinner 用法 1.
953 0
jQuery EasyUI API 中文文档 - 时间微调器（TimeSpinner）
TimeSpinner 时间微调器 扩展自 $.fn.spinner.defaults，用$.fn.timespinner.defaults 重写了 defaults。 依赖 spinner 用法 1. 1. \$('#ss').timespinner({   2.     showSeconds:true 3. });  特性 其特性扩展自 spinner，下列是为 timespinner 增加的特性。
736 0

1114 0

前言：有网友在问答网站Quora上提问：“有哪些省时小技巧，是每个Linux用户都应该知道的？” Joshua Levy 平常就在 Linux 平台工作，并且他积累了不少实用命令行技巧，他在回复中精选出一部分。对技术用户来说，这些技巧挺重要或实用，但知道的人并不多。下文略有点长，一般来说，用户也不需要对全部内容都了解，但为了达到省时方便的目的，Joshua Levy 仍不遗余力做了校对，以保证列出的每一条都值得一读，前提是你是一位Linux重度用户。
42 0
+关注

2107

1103

《2021云上架构与运维峰会演讲合集》

《零基础CSS入门教程》

《零基础HTML入门教程》