java 给出了很多整数范围;合并重叠范围。通过对范围进行排序来确定运行时间,因此时间复杂度为n lg(n)。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 给出了很多整数范围;合并重叠范围。通过对范围进行排序来确定运行时间,因此时间复杂度为n lg(n)。相关的知识,希望对你有一定的参考价值。

/**
 * You are given a sorted list of disjoint intervals and an interval, e.g. [(1, 5), (10, 15), (20, 25)] and *(12, 27).
 * Your task is to merge *them into a sorted list of disjoint intervals: [(1, 5), (10, 27)].
 */


/** there is not internal synchronisation in this class */

import java.util.*;

public class RangeSetBuilder {

    private Set<IntRange> ranges = new TreeSet<>();

    public static class IntRange implements Comparable<IntRange> {
        int start;
        int end;

        public IntRange(int start, int end) {
            this.start = start;
            this.end = end;
        }

        public IntRange(IntRange source) {
            this.start = source.start;
            this.end = source.end;
        }

        @Override
        public int compareTo(IntRange other) {
            return start - other.start;
        }

        private boolean intersects(IntRange other) {
            return other.end >= start && end >= other.start;
        }
    }

    public void addRange(IntRange range) {
        ranges.add(range);
    }

    public List<IntRange> compressedRange() {
        List<IntRange> compressedSet = new ArrayList<>();
        IntRange previous = null;
        for (IntRange event : ranges) {
            if (previous == null || !previous.intersects(event)) {
                previous = new IntRange(event);
                compressedSet.add(previous);
            }
            else
                previous.end = Math.max(previous.end, event.end);
        }
        return compressedSet;
    }


    public void printCompressedView() {
        List<IntRange> ranges = compressedRange();
        System.out.print("[");
        for (IntRange r : ranges)
            System.out.print("  ( " + r.start + "-" + r.end + " )  ");
        System.out.print("]");
        System.out.println();
    }

    public static void main(String... args) {
        RangeSetBuilder builder = new RangeSetBuilder();
        builder.addRange(new RangeSetBuilder.IntRange(1, 5));
        builder.addRange(new RangeSetBuilder.IntRange(10, 15));
        builder.addRange(new RangeSetBuilder.IntRange(20, 25));
        builder.addRange(new RangeSetBuilder.IntRange(12, 27));
        builder.printCompressedView();
        builder.addRange(new RangeSetBuilder.IntRange(5, 9));
        builder.printCompressedView();
        builder.addRange(new RangeSetBuilder.IntRange(27, 39));
        builder.printCompressedView();
        builder.addRange(new RangeSetBuilder.IntRange(9, 10));
        builder.printCompressedView();
    }

}

以上是关于java 给出了很多整数范围;合并重叠范围。通过对范围进行排序来确定运行时间,因此时间复杂度为n lg(n)。的主要内容,如果未能解决你的问题,请参考以下文章

合并PHP数组中的重叠范围?

在 SQL 中检测和合并日期范围的连续重叠

SQL Server 2014 合并重叠日期范围

在 C++ 中合并范围

56. Merge Intervals - LeetCode

给定 2 个整数列表如何找到不重叠的范围?