要检查重叠,在两个不同的dateranges,{Start1, End1}和{Start2, End2}我检查:
{Start1, End1}
{Start2, End2}
if ((Start1 <= End2) && (End1 >= Start2)) { //overlap exists }
问题是, 如果让我说五个日期范围,比较重叠的最佳方法 是 什么? 。
检查它们是否彼此不重叠?
如果我有多个日期范围,如何查找这些范围是否重叠?
查找是否全部重叠
static bool Overlap(params Tuple<DateTime, DateTime>[] ranges) { for (int i = 0; i < ranges.Length; i++) { for (int j = i + 1; j < ranges.Length; j++) { if (!(ranges[i].Item1 <= ranges[j].Item2 && ranges[i].Item2 >= ranges[j].Item1)) return false; } } return true; }
查找是否有重叠
static bool Overlap(params Tuple<DateTime, DateTime>[] ranges) { for (int i = 0; i < ranges.Length; i++) { for (int j = i + 1; j < ranges.Length; j++) { if (ranges[i].Item1 <= ranges[j].Item2 && ranges[i].Item2 >= ranges[j].Item1) return true; } } return false; }