您的位置:首页 > 其它

LeetCode: Merge Intervals 解题报告

2014-10-26 20:45 369 查看
[b]

/**
* 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 List<Interval> merge(List<Interval> intervals) {
List<Interval> ret = new ArrayList<Interval>();
if (intervals == null || intervals.size() == 0) {
return ret;
}

Collections.sort(intervals, new Comparator<Interval>() {
public int compare(Interval o1, Interval o2) {
// sort the intervals by the start.
return o1.start - o2.start;
}
});

// 作为最后一个插入的区间
Interval last = intervals.get(0);

// 这里要考虑性能。使用iterator的话,对linkedlist会更快.
Iterator<Interval> itor = intervals.iterator();
while (itor.hasNext()) {
Interval cur = itor.next();
// cur 在last的右边
if (cur.start > last.end) {
// 将cur作为新的last.
ret.add(last);
last = cur;
// cur与last有重合的部分,合并之
} else {
int s = last.start;
int e = Math.max(last.end, cur.end);
last = new Interval(s, e);
}
}

// 把最后一个区间加上
ret.add(last);

return ret;
}
}


View Code

[b]GITHUB CODE


REF: /article/1346945.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: