首页 > 代码库 > [LeetCode]Merge Intervals
[LeetCode]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]
.
/** * 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> res = new ArrayList<>(); for(int i=0;i<intervals.size();i++){ res=insert(res,intervals.get(i)); } return res; } private List<Interval> insert(List<Interval> intervals, Interval newInterval) { List<Interval> list = new ArrayList<Interval>(); if(intervals.size()==0){ list.add(newInterval); return list; } int start = newInterval.start; int end = newInterval.end; int mstart=-1; int mend=-1; int nstart = -1; int nend = -1; for(int i=0;i<intervals.size();i++){ Interval in = intervals.get(i); Interval next = null; if(i!=intervals.size()-1) { next= intervals.get(i+1); } if(in.start<=start&&start<=in.end){ mstart = i; nstart = in.start; break; }else if(i!=intervals.size()-1&&in.end<start&&start<next.start){ mstart = i+1; nstart = start; break; } } for(int i=0;i<intervals.size();i++){ Interval in = intervals.get(i); Interval next = null; if(i!=intervals.size()-1) { next= intervals.get(i+1); } if(in.start<=end&&end<=in.end){ mend = i; nend = in.end; break; }else if(i!=intervals.size()-1&&in.end<end&&end<next.start){ mend = i; nend = end; break; } } if(mstart==-1&&mend ==-1){ if(start>intervals.get(intervals.size()-1).end){ list = intervals; list.add(newInterval); }else if(end<intervals.get(0).start){ list = intervals; list.add(0, newInterval); }else{ list.add(newInterval); } return list; }else if(mstart==-1){ mstart = 0; nstart = start; list = intervals.subList(mend+1, intervals.size()); list.add(0,new Interval(nstart,nend)); return list; }else if(mend == -1){ mend = list.size()-1; nend = end; list = intervals.subList(0, mstart); list.add(new Interval(nstart,nend)); return list; } list.addAll(intervals.subList(0, mstart)); list.add(new Interval(nstart,nend)); list.addAll(intervals.subList(mend+1, intervals.size())); return list; } }
[LeetCode]Merge Intervals
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。