如何确定两个区间是否有交集?

问题:

假设有两个区间M(s1, e1)和N(s2, e2),如何判断两个区间是否有交集?

解答:

命题A: M和N相离,且M在前N在后。(当 s2 > e1时成立)

命题B: M和N相离,且M在后N在前。  ( 当 s1 > e2时成立)

如果命题(A or B)为真,则两个区间每有交集。

则如果命题(A or B)都为假,则两个区间有交集。

根据德摩根定律可知:

!(A or B) = !A and !B

也就是说 (s2 <= e1) and (s1 <= e2)

原文:http://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap

本文章迁移自http://blog.csdn.net/timberwolf_2012/article/details/23614635

/** * RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS. * LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/ /* var disqus_config = function () { this.page.url = PAGE_URL; // Replace PAGE_URL with your page's canonical URL variable this.page.identifier = PAGE_IDENTIFIER; // Replace PAGE_IDENTIFIER with your page's unique identifier variable }; */ (function() { // DON'T EDIT BELOW THIS LINE var d = document, s = d.createElement('script'); s.src = 'https://chenzz.disqus.com/embed.js'; s.setAttribute('data-timestamp', +new Date()); (d.head || d.body).appendChild(s); })();