這題是我每日連續第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
}