leetcode 57 重叠区域
This commit is contained in:
112
57.insert-interval.java
Normal file
112
57.insert-interval.java
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
/*
|
||||||
|
* @lc app=leetcode id=57 lang=java
|
||||||
|
*
|
||||||
|
* [57] Insert Interval
|
||||||
|
*/
|
||||||
|
|
||||||
|
// @lc code=start
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public int[][] insert(int[][] intervals, int[] newInterval) {
|
||||||
|
return insertSpecific(intervals, newInterval);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[][] insertSpecific(int[][] intervals, int[] newInterval) {
|
||||||
|
List<int[]> list = new ArrayList<>();
|
||||||
|
boolean appended = false;
|
||||||
|
for(int[] interval : intervals) {
|
||||||
|
if(appended) {
|
||||||
|
list.add(interval);
|
||||||
|
} else {
|
||||||
|
if(!isIntervalsOverfliped(interval, newInterval)) {
|
||||||
|
// list.add(interval);
|
||||||
|
if(newInterval[0] < interval[0]) {
|
||||||
|
appended = true;
|
||||||
|
list.add(newInterval);
|
||||||
|
list.add(interval);
|
||||||
|
} else {
|
||||||
|
list.add(interval);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
newInterval = joinIntervals(newInterval, interval);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!appended) {
|
||||||
|
list.add(newInterval);
|
||||||
|
}
|
||||||
|
// build ret
|
||||||
|
int[][] r = new int[list.size()][2];
|
||||||
|
for(int i = 0; i< list.size(); i++) {
|
||||||
|
r[i][0] = list.get(i)[0];
|
||||||
|
r[i][1] = list.get(i)[1];
|
||||||
|
}
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isIntervalsOverfliped(int[] i1, int[] i2) {
|
||||||
|
return !(i1[0] > i2[1] || i1[1] < i2[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private int[] joinIntervals(int[] i1, int[] i2) {
|
||||||
|
int[] r = new int[2];
|
||||||
|
r[0] = Math.min(i1[0], i2[0]);
|
||||||
|
r[1] = Math.max(i1[1], i2[1]);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[][] insertCommon(int[][] intervals, int[] newInterval) {
|
||||||
|
List<End> edges = new ArrayList<>();
|
||||||
|
for(int i = 0; i < intervals.length; i++) {
|
||||||
|
edges.add(new End(intervals[i][0], true));
|
||||||
|
edges.add(new End(intervals[i][1], false));
|
||||||
|
}
|
||||||
|
edges.add(new End(newInterval[0], true));
|
||||||
|
edges.add(new End(newInterval[1], false));
|
||||||
|
edges.sort((a, b)->{
|
||||||
|
if(a.pos!=b.pos) {
|
||||||
|
return a.pos - b.pos;
|
||||||
|
}
|
||||||
|
return (a.isStart?0:1) - (b.isStart?0:1);
|
||||||
|
});
|
||||||
|
List<int[]> r = new ArrayList<int[]>();
|
||||||
|
int c = 0, f=0;
|
||||||
|
for(End e: edges) {
|
||||||
|
if(c == 0) {
|
||||||
|
if(!e.isStart) {
|
||||||
|
throw new RuntimeException("invalid input");
|
||||||
|
}
|
||||||
|
c++;
|
||||||
|
f = e.pos;
|
||||||
|
} else {
|
||||||
|
if(e.isStart) {
|
||||||
|
c++;
|
||||||
|
} else {
|
||||||
|
c--;
|
||||||
|
if(c == 0) {
|
||||||
|
r.add(new int[]{f, e.pos});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int[][] ra = new int[r.size()][2];
|
||||||
|
for(int i=0; i<r.size(); i++) {
|
||||||
|
ra[i] = r.get(i);
|
||||||
|
}
|
||||||
|
return ra;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class End {
|
||||||
|
public int pos;
|
||||||
|
public boolean isStart;
|
||||||
|
|
||||||
|
public End(int pos, boolean isStart) {
|
||||||
|
this.pos = pos;
|
||||||
|
this.isStart = isStart;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// @lc code=end
|
||||||
|
|
||||||
Reference in New Issue
Block a user