###### 间隔问题2 [No. 57]

	/*
* s1: start point of interval 1
* e1: end point of interval 1
* s2: start point of interval 2
* e2: end point of interval 2
*/
public boolean overlap(int s1, int e1, int s2, int e2) {
return Math.max(s1, s2) < Math.min(e1, e2);
}

public class Test {
//check whether the two intervals intersect
public boolean overlap(Interval i1, Interval i2) {
return Math.max(i1.start, i2.start) < Math.min(i1.end, i2.end);
}

//merge the intervals and return a new interval list without overlapping
public void merge(ArrayList<Interval> list) {
int currentPosition = 0;
for (int i = 1; i < list.size(); i++) {
//no intersection
if (overlap(list.get(i), list.get(currentPosition)) == false) {
currentPosition++;
} else {
//get the new interval
int start = Math.min(list.get(i).start, list.get(currentPosition).start);
int end = Math.max(list.get(i).end, list.get(currentPosition).end);
Interval interval = new Interval(start, end);
list.remove(currentPosition);
}
}

int size = list.size();
for (int i = currentPosition + 1; i < size; i++) {
list.remove(currentPosition + 1);
}
}

public static void main(String[] args) {
ArrayList<Interval> list = new ArrayList<Interval>();

Collections.sort(list);

new Test().merge(list);
for(Interval inter : list) {
System.out.println("[" + inter.start + ", " + inter.end + "]");
}
}
}

class Interval implements Comparable<Interval>{
int start;
int end;

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

public int compareTo(Interval o) {
if (start > ((Interval)o).start ||
(start == ((Interval)o).start && end > ((Interval)o).end)) {
return 1;
} else if (start == ((Interval)o).start && end == ((Interval)o).end) {
return 0;
}
return -1;
}
}

#### 间隔问题 [No. 56]

2012-03-28 09:12:59

#### 间隔问题3 [No. 58]

2012-03-29 10:31:22

#### 57.插入间隔

2017-08-14 11:53:55

#### 【机器学习-斯坦福】学习笔记7 - 最优间隔分类器问题

2013-11-21 20:08:01

#### mysql导入报 Error Code: 2006 - MySQL 服务器已离线错误

2013-12-26 15:48:05

#### Leetcode #57. Insert Interval 间隔插入 解题报告

2016-04-27 23:56:44

#### 支持向量机（SVM）关键点攻略（最大间隔，对偶问题）

2016-03-26 21:40:10

#### SAP常见问题与解决办法 1

2011-08-23 11:28:56

#### Error Code: 1360 - Trigger does not existQuery

2015-04-12 22:53:17

#### CSA C22.2 NO.601.1

2014年07月23日 2.08MB 下载