检测重叠周期的算法 [英] Algorithm to detect overlapping periods

查看:241
本文介绍了检测重叠周期的算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须检测两个时间段是否重叠。

I've to detect if two time periods are overlapping.

每个时段都有开始日期和结束日期。

Every period has a start date and an end date.

我需要检测我的第一个时间段(A)是否与另一个(B / C)重叠。

I need to detect if my first time period (A) is overlapping with another one(B/C).

在我的情况下,如果B的开始等于A的结尾,它们不重叠(反之亦然)

In my case, if the start of B is equal to the end of A, they are not overlapping(the inverse too)

我发现以下情况:

所以其实我这样做是这样的:

So actually I'm doing this like this:

tStartA < tStartB && tStartB < tEndA //For case 1
OR
tStartA < tEndB && tEndB <= tEndA //For case 2
OR
tStartB < tStartA  && tEndB > tEndA //For case 3

(案例4在第一种情况下被考虑或在情况下2)

(The case 4 is taken in account either in case 1 or in case 2)

的作品,但似乎效果不佳。

首先是在c#中有一个现有的类可以建模这个(一段时间),就像一个timepsan,但有一个固定的开始日期。

So, first is there an existing class in c# that can modelize this(a time period), something like a timepsan, but with a fixed start date.

其次:是吗已经是ac#代码(像在Datetime类中)可以处理这个?

Secondly: Is there already a c# code(like in the Datetime class) which can handle this?

第三:如果没有,你的方法是使这个比较最快? / p>

Third: if no, what would be your approach to make this comparison the most fast?

推荐答案

简单检查两个时间段是否重叠:

Simple check to see if two time periods overlap:

bool overlap = a.start < b.end && b.start < a.end;

或您的代码:

bool overlap = tStartA < tEndB && tStartB < tEndA;

(使用< = code> 如果您改变主意想要说两个互相碰触的时期重叠。)

(Use <= instead of < if you change your mind about wanting to say that two periods that just touch each other overlap.)

这篇关于检测重叠周期的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆