From 1bf9d0ab4515d9d22d20e9be85189233be426c1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BD=A9=E8=BE=95=E9=BE=99=E5=84=BF?= Date: Tue, 18 Jan 2022 14:20:04 +0800 Subject: [PATCH] =?UTF-8?q?539:=E6=9C=80=E5=B0=8F=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E5=B7=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/cn/MinimumTimeDifference.java | 74 +++++++++++++++++++ .../cn/doc/content/MinimumTimeDifference.md | 27 +++++++ 2 files changed, 101 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/MinimumTimeDifference.java create mode 100644 src/main/java/leetcode/editor/cn/doc/content/MinimumTimeDifference.md diff --git a/src/main/java/leetcode/editor/cn/MinimumTimeDifference.java b/src/main/java/leetcode/editor/cn/MinimumTimeDifference.java new file mode 100644 index 0000000..b1c3a74 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/MinimumTimeDifference.java @@ -0,0 +1,74 @@ +//给定一个 24 小时制(小时:分钟 "HH:MM")的时间列表,找出列表中任意两个时间的最小时间差并以分钟数表示。 +// +// +// +// 示例 1: +// +// +//输入:timePoints = ["23:59","00:00"] +//输出:1 +// +// +// 示例 2: +// +// +//输入:timePoints = ["00:00","23:59","00:00"] +//输出:0 +// +// +// +// +// 提示: +// +// +// 2 <= timePoints.length <= 2 * 10⁴ +// timePoints[i] 格式为 "HH:MM" +// +// Related Topics 数组 数学 字符串 排序 👍 151 👎 0 + +package leetcode.editor.cn; + +import java.util.List; + +//539:最小时间差 +public class MinimumTimeDifference { + public static void main(String[] args) { + Solution solution = new MinimumTimeDifference().new Solution(); + // TO TEST + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int findMinDifference(List timePoints) { + int[] times = new int[2880]; + for (String timePoint : timePoints) { + String[] strs = timePoint.split(":"); + int time = Integer.parseInt(strs[0]) * 60 + Integer.parseInt(strs[1]); + if (times[time] == 1) { + return 0; + } + times[time] = 1; + times[time + 1440] = 1; + } + if (times[0] == 1 && times[1439] == 1) { + return 1; + } + int min = 1440; + int bef = 0; + for (int i = 1; i < 2880; i++) { + if (times[i] == 1) { + if (bef > 0) { + min = Math.min(min, i - bef); + } + if (i > 1439) { + break; + } + bef = i; + } + } + return min; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} diff --git a/src/main/java/leetcode/editor/cn/doc/content/MinimumTimeDifference.md b/src/main/java/leetcode/editor/cn/doc/content/MinimumTimeDifference.md new file mode 100644 index 0000000..8977c78 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/MinimumTimeDifference.md @@ -0,0 +1,27 @@ +

给定一个 24 小时制(小时:分钟 "HH:MM")的时间列表,找出列表中任意两个时间的最小时间差并以分钟数表示。

+ +

 

+ +

示例 1:

+ +
+输入:timePoints = ["23:59","00:00"]
+输出:1
+
+ +

示例 2:

+ +
+输入:timePoints = ["00:00","23:59","00:00"]
+输出:0
+
+ +

 

+ +

提示:

+ + +
Related Topics
  • 数组
  • 数学
  • 字符串
  • 排序

  • 👍 151
  • 👎 0
  • \ No newline at end of file