我试图找到两个范围(int 值)之间的交集,并且(如果存在)返回一个包含交集起点和终点的数组
例子
range 1 : 2,5
range 2 : 4,7
result : 4,5
我发现了其他几个关于数组之间交集的主题,但没有人帮助我找到确切的交集 (我刚刚发现一个有用的函数,如果交集存在但不告诉交集是什么,它会返回“真”)
我不擅长算法,所以我发现了一些问题,非常感谢您的提示
谢谢
最佳答案
这只是一些基本逻辑。
struct range
int start , end
range intersection(range a , range b)
//get the range with the smaller starting point (min) and greater start (max)
range min = (a.start < b.start ? a : b)
range max = (min == a ? b : a)
//min ends before max starts -> no intersection
if min.end < max.start
return null //the ranges don't intersect
return range(max.start , (min.end < max.end ? min.end : max.end))
关于javascript - 在javascript中获取两个范围之间的交集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32050984/