mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 04:54:51 +08:00
添加0056.合并区间Java解法
This commit is contained in:
@ -157,6 +157,28 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
```java
|
||||
// 版本2
|
||||
class Solution {
|
||||
public int[][] merge(int[][] intervals) {
|
||||
LinkedList<int[]> res = new LinkedList<>();
|
||||
Arrays.sort(intervals, (o1, o2) -> Integer.compare(o1[0], o2[0]));
|
||||
res.add(intervals[0]);
|
||||
for (int i = 1; i < intervals.length; i++) {
|
||||
if (intervals[i][0] <= res.getLast()[1]) {
|
||||
int start = res.getLast()[0];
|
||||
int end = Math.max(intervals[i][1], res.getLast()[1]);
|
||||
res.removeLast();
|
||||
res.add(new int[]{start, end});
|
||||
}
|
||||
else {
|
||||
res.add(intervals[i]);
|
||||
}
|
||||
}
|
||||
return res.toArray(new int[res.size()][]);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
```python
|
||||
|
Reference in New Issue
Block a user