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 ...
随机推荐
- BZOJ 1191 超级英雄 Hero 题解
BZOJ 1191 超级英雄 Hero 题解 Description 现在电视台有一种节目叫做超级英雄,大概的流程就是每位选手到台上回答主持人的几个问题,然后根据回答问题的多少获得不同数目的奖品或奖金 ...
- HDU 4751 Divide Groups
题目链接 比赛时候,建图建错了.大体算法想到了,不过很多细节都没想好. #include <cstdio> #include <cstring> #include <cm ...
- FPGA技术的发展历史和动向
本文关键字:fpga技术,fpga发展, fpga培训,FPGA应用开发入门与典型实例 一.FPGA技术的发展历史 纵观数字集成电路的发展历史,经历了从电子管.晶体管.小规模集成电路到大规模以及超大规 ...
- winform退出或关闭窗体时弹窗提示代码:转
winform退出或关闭窗体时弹窗提示代码,当我们点击窗体的 X 按钮时,会弹出一个对话框,询问我们是直接退出,还是最小化到托盘,还是取消这个行为.或是是否保存当前修改等等.以下以最小化到托盘为例. ...
- java list 交集 并集 差集 去重复并集
package com; import java.util.ArrayList;import java.util.Iterator;import java.util.List; public clas ...
- java读取邮件
package com.zz.mail; import java.io.FileNotFoundException; import java.io.FileOutputStream; import j ...
- 关闭CentOS不必要的开机启动项
命令行: for i in `chkconfig --list |grep 3:on|awk '{print $1}' |grep -Ev "network|sshd|sysstat|ude ...
- hao123列表的实现
<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" co ...
- ThinkPHP的URL优化
在刚刚安装好tp框架时,我们的路径一般是这样的: http://localhost/index.php/Home/Index/index?name=cly 我们可以通过下面一步步的步骤来优化这个路径 ...
- redis 安装(centos 6.4)
我使用6.4系统,mark一下,其他版本应该也一样. wget wget http://download.redis.io/releases/redis-3.0.6.tar.gz make make ...