c语言一维数组怎么快速排列
289
2022-08-29
LeetCode-539. 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:
The number of time points in the given list is at least 2 and won't exceed 20000.The input time is legal and ranges from 00:00 to 23:59.
题解:
排序后查找
go:
func min(a, b int) int { if a < b { return a } return b}func abs(a int) int { if a < 0 { return -a } return a}func findMinDifference(timePoints []string) int { day := 60 * 24 res := day n := len(timePoints) t := make([]int, n) for i := 0; i < n; i++ { hour := timePoints[i][0:2] minute := timePoints[i][3:5] h, _ := strconv.Atoi(hour) m, _ := strconv.Atoi(minute) cur := 60 * h + m t[i] = cur } sort.Ints(t) for i := 0; i < n; i++ { left := (i + n - 1) % n right := (i + 1) % n cur := min(min(abs(t[i] - t[left]), abs(day - abs(t[i] - t[left]))), min(abs(t[right] - t[i]), abs(day - abs(t[right] - t[i])))) res = min(res, cur) } return res}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~