1 回答

TA貢獻1875條經驗 獲得超3個贊
就是把同個區(qū)間的時間分在一組吧,很簡單,先排好序,再找出開始比前一個時間段的結尾要后的就行。
假設時間以 Number 方式存(距離 1 January 1970 00:00:00 UTC 的毫秒數(shù))
時間段結構:
{
start: 1493125454502,
end: 1493125454516
}
function sortTime (times) {
if (times.length <= 1) { return times }
times = times.sort((a, b) => a.start !== b.start ? a.start - b.start : a.end - b.end)
let result = []
let beginIndex = 0
for (let i = 1; i < times.length; i += 1) {
if (times[i].start > times[i - 1].end) {
result.push(times.slice(beginIndex, i))
beginIndex = i
}
}
if (beginIndex !== times.length) {
result.push(times.slice(beginIndex, times.length))
}
return result
}
添加回答
舉報