You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
vargetIntersection=functiongetIntersection(...secs){letlen=secs.length,ans=null;if(len===0)returnans;if(len===1)returnsecs[0].sort((a,b)=>a-b);ans=secs[0];// 以第一个区间作为参照for(vari=1,l=secs.length;i<l;i++){// 无交集 ans 最大值小于当前区间的最小值 / ans 的最小值大于当前区间的最大值if(Math.min(...secs[i])>Math.max(...ans)||Math.min(...ans)>Math.max(...secs[i])){returnnull;}ans=[Math.max(Math.min(...ans),Math.min(...secs[i])),Math.min(Math.max(...ans),Math.max(...secs[i]))];}returnans;};
vargetIntersection=functiongetIntersection(...secs){letlen=secs.length,ans=null;if(len===0)returnans;if(len===1)returnsecs[0].sort((a,b)=>a-b);ans=secs[0];// 以第一个区间作为参照for(vari=1,l=secs.length;i<l;i++){// 无交集 ans 最大值小于当前区间的最小值 / ans 的最小值大于当前区间的最大值if(Math.min(...secs[i])>Math.max(...ans)||Math.min(...ans)>Math.max(...secs[i])){returnnull;}ans=[Math.max(Math.min(...ans),Math.min(...secs[i])),Math.min(Math.max(...ans),Math.max(...secs[i]))];}returnans;};
更改 return 条件
+ if (len === 0 || len === 1) return ans;- if () return secs[0].sort((a, b) => a - b);
Activity
CodeRookie262 commentedon Jan 29, 2021
yokuray commentedon Jan 29, 2021
CodeRookie262 commentedon Jan 29, 2021
@Mirai39 优秀,不过我觉得只有一个区间的话应该直接返回出来,而不是null。
yokuray commentedon Jan 29, 2021
是指这行吗?但是如果只给一个区间的话就不存在交集啊
CodeRookie262 commentedon Jan 29, 2021
我觉得它和它本身就是一个交集,就和你要求最大值一样,只有一个数值的话那么这个数值就是最大的,而不是得有另外一个参照值。
yokuray commentedon Jan 29, 2021
O(∩_∩)O哈哈~,我觉得这跟最大值有点不一样,因为既然是交集的话那么必然要有参照,可以联想下“两条线交叉”,如果只有一条线,很难想象可以跟交叉联系到一起
CodeRookie262 commentedon Jan 29, 2021
@Mirai39 嗯,是我对交集的理解不太对,它是需要两个或两个以上的区间对比才成立的哈哈😝😝
CodeRookie262 commentedon Jan 29, 2021
更改 return 条件
yokuray commentedon Jan 29, 2021
一起加油(//̀Д/́/)