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】1987: Zju2672 Fibonacci Subsequence
题意 给出一个序列\(A\),求一个最长的满足fib性质的子序列,输出其长度及其元素(如果多种方案,输出位置最靠前的).(\(n \le 3000\)) 题解 容易想到dp,即\(d(i, j)\)表 ...
- 瀑布流 &留言板
实例:瀑布流 留言板(一)瀑布流瀑布流实现原理分析1.ajax文件内容function ajax(method, url, data, success) { var xhr = null; ...
- 19.创建如下三个类:(People类中的三个方法分别输出一些信息,ChinaPeople 和AmericanPeople类重写父类的三个方法)。
package zuoye2; public class People { protected double height; protected double weight; private Stri ...
- Java 中的转义字符
注意斜杠方向,为键盘右上角的斜杠 \t 在当前编辑位置插入一个 tab \b 在当前编辑位置插入一个空格 \n 换行(在当前编辑位置插入 a newline) \r 在当前编辑位置插入一个回车 ...
- 在html中如何获取表单提交的数据
a.html: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www ...
- 支付宝APP支付之Java后台生成签名具体步骤
/** *支付宝支付 * @param orderId 订单编号 * @param actualPay 实际支付金额 * @return */ private String getOrderInfoB ...
- Oracle数据库更新时间的SQL语句
---Oracle数据库更新时间字段数据时的sql语句---格式化时间插入update t_user u set u.name='pipi',u.modifytime=to_date('2015-10 ...
- Ajax Post 与 Get 实例
Ajax的POST实例,index.html <html> <head> <script type="text/javascript"> fun ...
- 累积进度图及本周PSP饼状图
每周进度 项目:词频统计 项目类型:个人项目 项目完成情况:已完成 C类别 C内容 S开始时间 E结束时间 I间隔 T净时间 分析 需求,设计 10:00 10:49 20 29 编码 代码的实 ...
- Algorithm | Tree traversal
There are three types of depth-first traversal: pre-order,in-order, and post-order. For a binary tre ...