什麼是檢查這個最有效的方法:在Java中檢查大於等於小於等於的最有效方法是什麼?
A is scheduled for 4 - 7;
B is scheduled for 5 - 8;
日程之上重疊。如何在Java中檢查,如果A和B在下面的例子中重疊或不:
對於
int start = 4;
int end = 7;
對B
int start = 5;
int end = 8;
請告知最有效的方法,以檢查是否重疊。 謝謝
什麼是檢查這個最有效的方法:在Java中檢查大於等於小於等於的最有效方法是什麼?
A is scheduled for 4 - 7;
B is scheduled for 5 - 8;
日程之上重疊。如何在Java中檢查,如果A和B在下面的例子中重疊或不:
對於
int start = 4;
int end = 7;
對B
int start = 5;
int end = 8;
請告知最有效的方法,以檢查是否重疊。 謝謝
最有效的方法是使用interval tree。
如果你只是處理一些數據,那麼即使只是比較開始和結束也足夠快。但是,如果您正在處理大數據,則可能需要使用其他數據結構(如增強樹)來獲得一些效率。
下面的邏輯應該可以測試重疊。這是從結束日期後退的負面離散檢查。其他檢查方法是採取參考開始時間,如data.getTime()
private boolean testOverlap(Date sched1Start, Date sched1End, Date sched2Start, Date sched2End) {
//Validate ranges to check if End dates are after start dates
//Overlaps if both schedules end at same time
if(sched1End.equals(sched2End)) return true;
//Get which one ends last
if(sched1End.before(sched2End)) {
//Working backwards sched2 starts after sched1 Ends so they dont overlap
if(sched2Start.after(sched1End) || sched2Start.equals(sched1End)) {
//Doesnt overlap
return false;
}
} else {
//Sched1 ends last
if(sched1Start.after(sched2End) || sched1Start.equals(sched2End)) {
//Doesnt overlap
return false;
}
}
return true;
}
你現在做得怎麼樣? –
與C語言類似,沒有「特殊」的方式來做到這一點 - 必須單獨測試每個綁定。 –
你有沒有一個你正在做什麼來克服這個問題的例子? – eddiecubed