检查 C# 中的重叠系列
我正在制作一个应用程序,用户添加开始和结束来定义范围
条件是范围不应重叠:
如何检查数字范围是否不重叠,例如
- 范围1开始5结束15
- 范围2开始1结束4
- 范围3 Start 16 End 20
- Range 4 Start 2 End 4
所以 Range 4 使设置无效,我如何在 C# 中检查这一点。
此外,用户可以按任何顺序添加范围,如上例所示,整个系列应该不重叠。
感谢您的帮助建议。
问候, Sakshi
回答:
我提出的解决方案是否正确: 如果 start 和 end 是需要验证的范围,则
- start >startRange 且 start 小于 endRange
- end>startRange 且 end 小于 endRange
上述 2 个条件验证该系列重叠。
其中 startRange 和 endRange 是所有现有范围的开始和结束。
I am making an application where the user adds start and end to define a range
The condition is that the range should not overlap:
How to check whether a number range is not overlapping e.g.
- Range 1 Start 5 End 15
- Range 2 Start 1 End 4
- Range 3 Start 16 End 20
- Range 4 Start 2 End 4
So the Range 4 makes the set invalid, how do I check this in C#.
Further the user can add the range in any order as in the example above, the entire series should be non overlapping.
Thanks for the help suggestion.
Regards,
Sakshi
Answer:
I made the solution is it correct:
If start and end is the range which needs to be validated then
- start >startRange and start less than endRange
- end>startRange and end less than endRange
The above 2 condition validates that the series is overlapping.
Where startRange and endRange is the start and end for all existing ranges.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
OP 建议进行两项检查来验证新范围不与现有范围重叠。然而,这两项检查并未涵盖所有可能性。例如,如果现有范围是 (4,10) 而新范围是 (2,12),则不会对其进行标记,因为它在现有范围的开始之前开始,并在现有范围之后结束。
相反,我建议采用以下方法:
本质上,重叠范围有四种可能性:
情况 (1) 和 (2) 包括部分重叠,而情况 (3) 和 (4) 包括完全重叠(无论是现有范围完全包含新范围 [情况3] 或新范围完全包围现有范围 [情况 4])。
OP 的代码捕获情况 1、2 和 3,但不捕获情况 4。此处的代码捕获重叠范围的所有 4 种可能性。
The OP suggests two checks to validate that a new range does not overlap with an existing range. However, these two checks do not cover all the possibilities. For instance, if the existing range is (4,10) and the new range is (2,12), it will not be flagged, because it starts prior to the start of the existing range, and ends afterwards.
Instead, I'd recommend the following approach:
Essentially, there are four possibilities of overlapping ranges:
Cases (1) and (2) include partial overlap, while cases (3) and (4) include complete overlap (either the existing range completely encloses the new range [case 3] or the new range completely encloses the existing range [case 4]).
The OP's code catches cases 1, 2 and 3, but not case 4. The code here catches all 4 possibilities of an overlapping range.
范围需要进行多次检查:
您可以有多种重叠变化,因此您需要进行多次测试。
首先检查:
范围开始 1 >= 范围 2 开始和范围 1 开始 <= 范围 2 结束
第二次检查:
范围 2 的起点 >= 范围 1 的起点和范围 2 的起点 <= 范围 1 的终点
第三次检查:
范围结束 1 >= 范围 2 开始且范围结束 1 <= 范围 2 结束
第四次检查:
范围结束 2 >= 范围 1 开始和范围结束 2 <= 范围 1 结束
这些检查假定两个范围上的结束 >= 开始。如果没有,您需要交换测试的开始和结束。
Ranges need to have multiple checks:
You can have many variations of overlap, so you'll need to do several tests.
First Check:
Start of Range 1 >= Start of Range 2 and Start of Range 1 <= End of Range 2
Second Check:
Start of Range 2 >= Start of Range 1 and Start of Range 2 <= End of Range 1
Third Check:
End of Range 1 >= Start of Range 2 and End of Range 1 <= End of Range 2
Fourth Check:
End of Range 2 >= Start of Range 1 and End of Range 2 <= End of Range 1
These checks assume that End >= Start on both ranges. If not, you'll need to swap the start and end for the tests.