码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode]Merge Intervals

时间:2014-10-14 22:48:09      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:java   leetcode   

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].

参照insert intervals

/**
 * 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

标签:java   leetcode   

原文地址:http://blog.csdn.net/guorudi/article/details/40085023

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!