此代碼返回重疊座標。 示例:輸入[10,30] [20,50] [40,70,] [60,90] [80,100] 答案應該是:[20,30],[40,50] [ 60,70] [80,90] 有沒有辦法解決這個問題的時間複雜度小於二次方?查找重疊間隔座標的複雜度降低
謝謝,
public static Set<OverlapCoord> getOverlap(List<Interval> intervalList) {
if (intervalList == null) {
throw new NullPointerException("Input list cannot be null.");
}
final HashSet<OverlapCoord> hashSet = new HashSet<OverlapCoord>();
for (int i = 0; i < intervalList.size() - 1; i++) {
final Interval intervali = intervalList.get(i);
for (int j = 0; j < intervalList.size(); j++) {
final Interval intervalj = intervalList.get(j);
if (intervalj.getStart() < intervali.getEnd() && intervalj.getEnd() > intervali.getStart() && i != j) {
hashSet.add(new OverlapCoord(Math.max(intervali.getStart(),intervalj.getStart()),
Math.min(intervali.getEnd(), intervalj.getEnd())));
}
}
}
return hashSet;
}
是否有可能超過兩個區間重疊? – Akinakes
是的,允許重疊。 – JavaDeveloper