leetcode56. 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].
解题思路:
1)将集合进行排序;
集合的排序建议使用Collections的sort方法实现,需要自己实现Comparator接口;
2)依次进行归并,当后者的start大于当前Interval的end时,将当前Interval加入到集合中,否则将当前Interval和后者Interval进行归并
注意:边界条件的判断
代码:
/**
* 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) {
//处理特殊情况
if(intervals == null || intervals.size() <2 ){
return intervals;
} //集合排序
ComparatorImpl com = new ComparatorImpl();
Collections.sort(intervals,com); //归并
List<Interval> temp = new ArrayList<Interval>();
Interval val = intervals.get(0);
Interval next;
boolean flag = true;
for(int i=1; i<intervals.size();){
next = intervals.get(i);
flag = true;
if(next.start > val.end){
temp.add(val);
val = next;
flag = false;
}else{
//进行一次归并,i自增一次
val.end = Math.max(val.end,next.end);
i++;
}
} //处理最后一个未归并的Interval
if(flag){
temp.add(val);
} return temp;
}
} //实现排序接口
class ComparatorImpl implements Comparator<Interval>
{
public int compare(Interval o1, Interval o2) {
// TODO Auto-generated method stub
return o1.start-o2.start;
} }
leetcode56. Merge Intervals的更多相关文章
- [array] leetcode-56. Merge Intervals - Medium
leetcode-56. Merge Intervals - Medium descrition Given a collection of intervals, merge all overlapp ...
- Leetcode56. Merge Intervals合并区间
给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: 区间 [1,3] ...
- 【leetcode】Merge Intervals
Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given ...
- 【leetcode】Merge Intervals(hard)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- 60. Insert Interval && Merge Intervals
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [Leetcode][Python]56: Merge Intervals
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...
- 【leetcode】 Merge Intervals
Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given ...
- Insert Interval & Merge Intervals
Insert Intervals Given a non-overlapping interval list which is sorted by start point. Insert a new ...
随机推荐
- 【JAVA】JMX简单使用方法
[BEAN] 配置 <!-- JMX 对应的接口服务--> <bean id="emailInterfaceServer" class="com.s ...
- HDU 1251 Trie树模板题
1.HDU 1251 统计难题 Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #incl ...
- spin_lock 和 spin_lock_irqsave
一 .spin_lock_irqsave . spin_unlock_irqrestore 如果自旋锁在中断处理函数中被用到,那么在获取该锁之前需要关闭本地中断,spin_lock_irqsave ...
- 最全的iOS面试题及答案-转载
1. Object-c的类可以多重继承么?可以实现多个接口么?Category是什么?重写一个类的方式用继承好还是分类好?为什么? 答: Object-c的类不可以多重继承:可以实现多个接口,通过实现 ...
- Net-SNMP是线程安全的吗
原文地址 : http://www.net-snmp.org/wiki/index.php/FAQ:General_19 Net-SNMP是线程安全的吗? 确切的说,不是.不过呢,在多线程管理的应用进 ...
- OSG+VS2010+win7环境搭建---OsgEarth编译
OSG+VS2010+win7环境搭建---OsgEarth编译 转:http://www.cnblogs.com/hnfxs/p/3161261.html Win7下 osg+vs2010环境搭建 ...
- 1063. Set Similarity (25)
1063. Set Similarity (25) 时间限制 300 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given ...
- Log打印日志遇到的问题
Log日志打印出现空指针问题 AndroidRuntime(372): Caused by: java.lang.NullPointerException: println needs a messa ...
- [LintCode] Reverse Pairs 翻转对
For an array A, if i < j, and A [i] > A [j], called (A [i], A [j]) is a reverse pair.return to ...
- 9.20 java继承
package liu0920; //继承 public class Person { //属性 姓名和年龄 private String name; private int age; //无参构造方 ...