Re: [闲聊] 每日leetcode

楼主: JIWP (JIWP)   2024-09-17 01:09:02
这题是我每日连续第300天
快满一年,可以不用继续写了吗
539. Minimum Time Difference
给你一个24小时制的时间清单
格式为"HH:MM"
请回传任意两个时间差的最小值
思路:
就先把时间转换成数字
接着排序一下
再来抓出差距最小的时间差
GOLANG CODE:
func findMinDifference(timePoints []string) int {
n := len(timePoints)
rec := make([]int, 0, n)
for _, time := range timePoints {
hours, _ := strconv.Atoi(time[0:2])
mins, _ := strconv.Atoi(time[3:])
rec = append(rec, hours*60+mins)
}
abs := func(a int) int {
if a < 0 {
return -a
}
return a
}
slices.Sort(rec)
prev, res := rec[0], 100000
for i := 1; i < n; i++ {
diff := abs(prev - rec[i])
if diff > 720 {
res = min(1440-diff, res)
} else {
res = min(diff, res)
}
prev = rec[i]
}
diff := abs(rec[0] - rec[n-1])
if diff > 720 {
res = min(1440-diff, res)
} else {
res = min(diff, res)
}
return res
}
作者: oin1104 (是oin的说)   2024-09-17 01:16:00
大师
作者: Rushia (みけねこ的鼻屎)   2024-09-17 01:38:00
我们要一辈子写LEETCODE

Links booklink

Contact Us: admin [ a t ] ucptt.com