标签:
https://leetcode.com/problems/merge-intervals/
Given a collection of intervals, merge all overlapping intervals.
For example,
Given [1,3],[2,6],[8,10],[15,18]
,
return [1,6],[8,10],[15,18]
.
解题思路:
这题应该没有hard的难度。思路如下:
1. 对原intervals的List根据start排序。
2. 遍历intervals,如果当前interval的start<=前一个interval的end,就合并它们,即将start设置为前一个interval的start,end设置为两者较大的。合并的结果作为前一个interval保存。否则,将前一个interval塞进结果list,当前前一个interval作为前一个interval保存。
3. 重复第2步,直至intervals结束。
还有些边界条件需要考虑,比如遍历到intervals的最后一个元素,无论合并与否,都要将结果加入到result里。
先使用了优先队列,也就是最小堆,占用了额外的O(n)空间。
/** * 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 { public class Compare implements Comparator<Interval> { public int compare(Interval l1, Interval l2){ return l1.start - l2.start; } } public List<Interval> merge(List<Interval> intervals) { List<Interval> result = new ArrayList<Interval>(); if(intervals == null || intervals.size() == 0) { return result; } PriorityQueue<Interval> queue = new PriorityQueue<Interval>(intervals.size(), new Compare()); for(int i = 0; i < intervals.size(); i++){ queue.add(intervals.get(i)); } Interval current = null; Interval previous = null; while(queue.size() > 0){ current = queue.poll(); if(previous == null) { previous = current; if(queue.size() == 0) { result.add(previous); } continue; } if(current.start > previous.end) { result.add(previous); previous = current; } else { current.start = previous.start; current.end = Math.max(current.end, previous.end); previous = current; } if(queue.size() == 0) { result.add(previous); } } return result; } }
后来感觉可以针对intervals排序,不用额外空间了。
/** * 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 { public class Compare implements Comparator<Interval> { public int compare(Interval l1, Interval l2){ return l1.start - l2.start; } } public List<Interval> merge(List<Interval> intervals) { List<Interval> result = new ArrayList<Interval>(); if(intervals == null || intervals.size() == 0) { return result; } Collections.sort(intervals, new Compare()); Interval current = null; Interval previous = null; for(int i = 0; i < intervals.size(); i++) { current = intervals.get(i); if(previous == null) { previous = current; if(i == intervals.size() - 1) { result.add(previous); } continue; } if(current.start > previous.end) { result.add(previous); previous = current; } else { current.start = previous.start; current.end = Math.max(current.end, previous.end); previous = current; } if(i == intervals.size() - 1) { result.add(previous); } } return result; } }
不管如何,时间复杂度都是O(n*logn),排序所花。
标签:
原文地址:http://www.cnblogs.com/NickyYe/p/4387459.html