mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
Update 0435.无重叠区间.md
更新Java版本代码
This commit is contained in:
@ -183,28 +183,22 @@ public:
|
|||||||
```java
|
```java
|
||||||
class Solution {
|
class Solution {
|
||||||
public int eraseOverlapIntervals(int[][] intervals) {
|
public int eraseOverlapIntervals(int[][] intervals) {
|
||||||
if (intervals.length < 2) return 0;
|
Arrays.sort(intervals, (a, b) -> {
|
||||||
|
if (a[0] == a[0]) return a[1] - b[1];
|
||||||
Arrays.sort(intervals, new Comparator<int[]>() {
|
return a[0] - b[0];
|
||||||
@Override
|
|
||||||
public int compare(int[] o1, int[] o2) {
|
|
||||||
if (o1[1] != o2[1]) {
|
|
||||||
return Integer.compare(o1[1],o2[1]);
|
|
||||||
} else {
|
|
||||||
return Integer.compare(o1[0],o2[0]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
int count = 1;
|
int count = 0;
|
||||||
int edge = intervals[0][1];
|
int edge = Integer.MIN_VALUE;
|
||||||
for (int i = 1; i < intervals.length; i++) {
|
for (int i = 0; i < intervals.length; i++) {
|
||||||
if (edge <= intervals[i][0]){
|
if (edge <= intervals[i][0]) {
|
||||||
count ++; //non overlap + 1
|
|
||||||
edge = intervals[i][1];
|
edge = intervals[i][1];
|
||||||
|
} else {
|
||||||
|
count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return intervals.length - count;
|
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
Reference in New Issue
Block a user