首先顶多有 24*60 个时间点。数组的数量超过这个的。说明有重复的时间点。直接返回0
这道题比较恶心啊。。因为00:35 他可以认为当天的35分钟 或者是第二天的35分
所以排序之后。在最后加上隔天的时间。来应对这个问题
func findMinDifference(_ timePoints: [String]) -> Int {
if timePoints.count > 24*60 {
return 0
}
var array = Array<Int>()
for s in timePoints {
array.append(dealWithStr(s))
}
array = array.sorted()
array.append(array[0]+24*60)
var min = Int.max
for i in 1..<array.count {
let value1 = array[i]
let value2 = array[i - 1]
let dif = abs(value1 - value2)
if dif < min {
min = dif
}
}
return min
}
func dealWithStr(_ str:String) -> Int {
var sum = 0
let tempStr = NSString(string: str)
let hour = tempStr.substring(with: NSMakeRange(0, 2))
let hourValue = (hour as NSString ).integerValue
let minute = tempStr.substring(with: NSMakeRange(3, 2))
let minuteValue = (minute as NSString ).integerValue
sum = hourValue * 60 + minuteValue
return sum
}
网友评论