此代码返回重叠坐标。示例:对于 [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;
}