問題:java
Given a collection of intervals, merge all overlapping intervals.app
For example,
Given [1,3],[2,6],[8,10],[15,18]
,
return [1,6],[8,10],[15,18]
.ide
解決:this
① 先將區間按照每一個start的值來排序,排好序之後判斷一個區間的start值是否處在前一個區間中,若是在前一個區間中,那麼合併;若是不在,就將新區間添加。spa
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution { //28ms
public List<Interval> merge(List<Interval> intervals) {
List<Interval> res = new ArrayList<>();
if(intervals.size() == 0)
return res;
// 根據start將區間排序,重載外部比較器
Collections.sort(intervals, new Comparator<Interval>(){
public int compare(Interval a, Interval b){
return a.start - b.start;
}
});
// 獲取第一個區間
Interval temp = intervals.get(0);
// 若是隻有一個區間
if(intervals.size() == 1){
res.add(temp);
return res;
}
// 從第一個開始遍歷區間
for(int i = 1; i < intervals.size(); i ++){
// case 1: 若是temp.end >= intervals.get(i).start,代表兩區間重合,更新temp的end爲max(tempEnd, thisEnd)
if(temp.end >= intervals.get(i).start){
temp.end = Math.max(temp.end, intervals.get(i).end);
}
// case 2: 兩個區間沒有重合
else{
res.add(temp);
temp = intervals.get(i);
}
}
//將最後一個區間加入到鏈表中
res.add(temp);
return res;
}
}.net
② 將start和end分別提取出來,而後進行排序而且分類。code
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
class Solution { //17ms
public List<Interval> merge(List<Interval> intervals) {
int len = intervals.size();
int[] starts = new int[len];
int[] ends = new int[len];
List<Interval> res = new ArrayList<>();
for(int i = 0;i < len;i ++){
starts[i] = intervals.get(i).start;
ends[i] = intervals.get(i).end;
}
Arrays.sort(starts);
Arrays.sort(ends);
for(int i = 0,j = 0;i < len;i ++){
if(i == len - 1 || starts[i + 1] > ends[i]){
res.add(new Interval(starts[j],ends[i]));
j = i + 1;
}
}
return res;
}
}對象
【注】java 兩種比較器Comparator 和 Comparable 排序
public class CompareTest {
public static void main(String[] args) {
List<Student> list=new ArrayList<Student>(10);
list.add(new Student(1,"jj"));
list.add(new Student(0,"ww"));
list.add(new Student(0,"kk"));
list.add(new Student(2,"ll"));
Collections.sort(list); //內部比較器:要排序的對象實現Comparable接口,能夠對自身進行比較
System.out.println(list);
List<Teacher> t=new ArrayList<Teacher>(10);
t.add(new Teacher(1,12));
t.add(new Teacher(0,13));
t.add(new Teacher(0,14));
t.add(new Teacher(2,15));
Collections.sort(t,new StudentComparator()); //外部比較器:經過實現Comparator接口
System.out.println(t);
}
}
class Student implements Comparable {
int num;
String name;
public Student(int num, String name) {
this.num = num;
this.name = name;
}
@Override
public String toString() {
return "\r\tnum:"+num+" name:"+name+"\r";
}
public int compareTo(Object o) {
Student tmp=(Student) o;
int result=tmp.num>num?1:(tmp.num==num?0:-1);
if (result==0) {
result=tmp.name.indexOf(0)>name.indexOf(0)?1:-1;
}
return result;
}
}
class Teacher{
int num;
double salary;
public Teacher(int num, double salary) {
this.num = num;
this.salary = salary;
}
@Override
public String toString() {
return "\r\tnum:"+num+" salary:"+salary+"\r";
}
}
class StudentComparator implements Comparator{
public int compare(Object o1, Object o2) {
Teacher t1=(Teacher) o1;
Teacher t2=(Teacher) o2;
int result=t1.num>t2.num?1:(t1.num==t2.num?0:-1);
return result=result==0?(t1.salary<t2.salary?1:-1):result;
}
} 接口