让我们想象一下:
您有一条从1开始到10000结束的行。
您将获得一系列[{ start: 5, end: 10 }, { start: 15, end: 25 }]
这样的范围
给出一个范围数组,求反。
对于上面的示例,倒数为[{ start: 1, end: 4 }, { start: 11, end: 14 }, { start: 26, end: 10000 }]
请注意,倒数基本上是我们行中其他所有区域的倒数。
下面是我当前的解决方案...是否有一个更优雅的解决方案,不必显式处理边缘情况?
请注意,在我的代码范围内是命名区域。
const inverseRegions = (regions) => {
// If no regions, the inverse is the entire line.
if (regions.length === 0) {
return [{ start: 1, end: 10000 }]
}
let result = []
// If the first region doesn't start at the beginning of the line
// we need to account for the region from the 1 to the start of
// first region
if (regions[0].start !== 1) {
result.push({
start: 1,
end: regions[0].start - 1
})
}
for (let i = 1; i < regions.length; i++) {
const previousRegion = regions[i-1]
const region = regions[i]
result.push({
start: previousRegion.end + 1,
end: region.start - 1
})
}
// If the last region doesn't end at the end of the line
// we need to account for the region from the end of the last
// region to 10000
if (regions[regions.length - 1].end !== 10000) {
result.push({
start: regions[regions.length - 1].end + 1,
end: 10000
})
}
return result
}
inverseRegions([])
=> [ { start: 1, end: 10000 } ]
inverseRegions([{ start: 1, end: 10 }, { start: 15, end: 20 }])
=> [ { start: 11, end: 14 },
{ start: 21, end: 10000 } ]
inverseRegions([{ start: 5, end: 10 }, { start: 12, end: 60 }, { start: 66, end: 10000 }])
=> [ { start: 1, end: 4 },
{ start: 11, end: 11 },
{ start: 61, end: 65 } ]
inverseRegions([{ start: 8, end: 12 }, { start: 16, end: 20 }, { start: 29, end: 51 }])
=> [ { start: 1, end: 7 },
{ start: 13, end: 15 },
{ start: 21, end: 28 },
{ start: 52, end: 10000 } ]
答案 0 :(得分:2)
您可以使用reduce
并使用包含整个区域的累加器进行初始化,然后在遇到新范围时将最后一个区域分割:
function inverseRegions(ranges) {
return ranges.reduce((acc, curr) => {
let prev = acc.pop();
if (curr.start > prev.start)
acc.push({start: prev.start, end: curr.start - 1})
if (prev.end > curr.end)
acc.push({start: curr.end + 1, end: prev.end});
return acc;
}, [{start: 1, end: 10000}])
}
console.log(inverseRegions([{ start: 5, end: 10 }, { start: 15, end: 25 }]));
console.log(inverseRegions([]));
console.log(inverseRegions([{ start: 1, end: 10 }, { start: 15, end: 20 }]));
console.log(inverseRegions([{ start: 5, end: 10 }, { start: 12, end: 60 }, { start: 66, end: 10000 }]));
console.log(inverseRegions([{ start: 8, end: 12 }, { start: 16, end: 20 }, { start: 29, end: 51 }]));
这当然是假设您的时间间隔已排序。