Leetcode 56. Merge Intervals & 57. Insert Interval
2016-04-27 05:30
423 查看
56. Merge Intervals
Total Accepted: 65710 TotalSubmissions: 259254 Difficulty: Hard
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].
思路:
先按区间start排序,需要建一个新的class implements Comparator来排序。
然后取俩interval,如果前者的end大于后者的start,则新建一个interval,然后把prev变成这个新建的interval,进行下次循环;否则直接输出prev到结果中。
/** * 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()<=1) return intervals; Collections.sort(intervals, new IntervalComparator()); ArrayList<Interval> result = new ArrayList<Interval>(); Interval prev = intervals.get(0); for (int i = 1; i < intervals.size(); i++) { Interval curr = intervals.get(i); if (prev.end >= curr.start) { // merged case Interval merged = new Interval(prev.start, Math.max(prev.end, curr.end)); prev = merged; } else { result.add(prev); prev = curr; } } result.add(prev); return result; } class IntervalComparator implements Comparator<Interval>{ public int compare(Interval i1, Interval i2){ return i1.start - i2.start; } } }
57. Insert Interval
Total Accepted: 57111 TotalSubmissions: 239980 Difficulty: Hard
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).
You may assume that the intervals were initially sorted according to their start times.
Example 1:
Given intervals
[1,3],[6,9], insert and merge
[2,5]in
as
[1,5],[6,9].
Example 2:
Given
[1,2],[3,5],[6,7],[8,10],[12,16], insert and merge
[4,9]in
as
[1,2],[3,10],[12,16].
This is because the new interval
[4,9]overlaps
with
[3,5],[6,7],[8,10].
先把new interval插入给定的intervals里面,然后直接用56题代码,搞定!
/** * 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> insert(List<Interval> intervals, Interval newInterval) { intervals.add(newInterval); Collections.sort(intervals, new IntervalComparator()); ArrayList<Interval> result = new ArrayList<Interval>(); Interval prev = intervals.get(0); for (int i = 1; i < intervals.size(); i++) { Interval curr = intervals.get(i); if (prev.end >= curr.start) { // merged case Interval merged = new Interval(prev.start, Math.max(prev.end, curr.end)); prev = merged; } else { result.add(prev); prev = curr; } } result.add(prev); return result; } public class IntervalComparator implements Comparator<Interval>{ public int compare(Interval i1, Interval i2){ return i1.start - i2.start; } } }
相关文章推荐
- round() Function in Python and Matlab
- Android中怎样使用计步器传感器 "TYPE_STEP_DETECTOR"
- Leetcode 41. First Missing Positive
- std::swap 不支持 std::auto_ptr 类型参数
- spring+sprinmvc+mybatis基本整合(二)--基于spring注解mybais非注解非Mapper接口方式
- mysql小知识笔记
- 晶体管电脑能够胜过神经元人脑吗?
- Leetcode 36. Valid Sudoku & 37. Sudoku Solver
- 国内90%以上的 iOS 开发者,对 APNs 的认识都是错的
- Struts2学习笔记之s:select标签
- linux下安装mysql5.6.26
- python使用matplotlib绘图
- IntelliJ IDEA安装 一些配置
- .Net开源网络爬虫Abot介绍
- CSS选择器
- html中的meta详解
- MVC生成CheckBoxList并对其验证
- nfs
- 【Swift学习】Swift编程之旅---类和结构体(十三)
- CSharpGL(20)用unProject和Project实现鼠标拖拽图元