编辑代码

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Comparator;
class Main {
    public static void main(String[] args) {
        //JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。
         SectionTemplate sectionTemplate=new SectionTemplate();
        sectionTemplate.test1();
        sectionTemplate.test2();
        sectionTemplate.test3();
    }
}
 class Section {
   public int start;
   public int end;

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

    Section(Section section) {
        this.start=section.start;
        this.end=section.end;
    }
}
 class SectionTemplate {
    public static LinkedList<Section> findSection(Section[] sections){
        LinkedList<Section> beans = new LinkedList<Section>();
        Arrays.sort(sections, new Comparator<Section>() {
            public int compare(Section o1, Section o2) {

                if (o1.end < o2.end){
                    return -1;
                }
                else if (o1.end == o1.end) {
                    return 0;
                }
                else {
                    return 1;
                }
            }
        });
        beans.add(sections[0]);
        for (int i = 1; i < sections.length; i++) {
            Section last = beans.getLast();
            if (last.end <= sections[i].start) {
               beans.add(sections[i]);
            }
        }

        return beans;
    }
    public static void test1() {
        Section[] sections = {
                new Section(6, 8),
                new Section(2, 4),
                new Section(3, 5),
                new Section(1, 5),
                new Section(5, 9),
                new Section(8, 10),
                new Section(5,7),
                new Section(7,9),
                new Section(9,10)
        };

        LinkedList<Section> beans = findSection(sections);

        double maxValue = 0;

        System.out.println("找到如下不相交的区间:");
        for (int i = 0; i < beans.size(); ++i) {
            System.out.println("(" + beans.get(i).start + "," + beans.get(i).end + ")");
        }

    }
    public static void test2() {
        Section[] sections = {
                new Section(6, 8),
                new Section(2, 4),
                new Section(3, 5),
                

        };

        LinkedList<Section> beans = findSection(sections);

        double maxValue = 0;

        System.out.println("找到不相交的区间:");
        for (int i = 0; i < beans.size(); ++i) {
            System.out.println("(" + beans.get(i).start + "," + beans.get(i).end + ")");
        }

    }
    public static void test3() {
        Section[] sections = {
                new Section(6, 8),
                new Section(2, 4),
                new Section(3, 5),
                new Section(1, 5),
                new Section(5, 9),
                new Section(8, 10),
                new Section(5,7),
                

        };

        LinkedList<Section> beans = findSection(sections);

        double maxValue = 0;

        System.out.println("找到如下不相交的区间:");
        for (int i = 0; i < beans.size(); ++i) {
            System.out.println("(" + beans.get(i).start + "," + beans.get(i).end + ")");
        }

    }
}