2015.03.30 LeetCode Merge Intervals 解题记录

今天下午做了一道题。leetcode merge intervals 属于比较难的题目。

首先用collections.sort 给list排序,然后用两个while loop来比较两个interval 的start, end 。 从而生成新的interal,再插入到新的list 返回结果。

下面给出自己的代码:

/*
 50 Merge Intervals 

 https://leetcode.com/problems/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; }
 * }
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import java.util.Comparator;

public class MergeIntervals {
	//  sort the List, and then compare two Interval in two while loops
	 public static List<Interval> merge(List<Interval> intervals) {
		 // Ref : http://gongxuns.blogspot.com/2012/12/leetcode-merge-intervals.html
		 List<Interval> res = new ArrayList<Interval>();
 		Collections.sort(intervals, new Comparator<Interval>(){
 		            public int compare(Interval p1, Interval p2) {
 		                 if (p1.start != p2.start )
							 	return (p1.start- p2.start );
						 else
							 return (p1.end - p2.end);
						}
 		        });
		printIntervals(intervals);
		int i=0;
		while(  i< intervals.size()) {
			int j= i+1;
			Interval left = intervals.get(i);
			int end = left.end;
			while (j< intervals.size()  && end >= intervals.get(j).start) {
				end = Math.max(end, intervals.get(j).end);
				j++;
			}
			res.add( new Interval(left.start, end));
			i=j;
		}
		// System.out.println("final");
		// printIntervals(res);
		return res;

	 }

	public static void main(String[] args) {
		Interval interval0 = new Interval(0,2);
		Interval interval1 = new Interval(4, 6);

		Interval interval2 = new Interval(1, 1);
		Interval interval3 = new Interval(5, 5);
		Interval interval4 = new Interval(3, 3);
		Interval interval5 = new Interval(5, 6);
		Interval interval6 = new Interval(0, 1);
		Interval interval7 = new Interval(0, 1);
		Interval interval8 = new Interval(1, 2);
		Interval interval9 = new Interval(5, 6);
		Interval interval10 = new Interval(5, 5);
		Interval interval11 = new Interval(0, 0);		

		//[[0,2],[4,6],[1,1],[5,5],[3,3],[5,6],[0,1],[0,1],[1,2],[5,6],[5,5],[0,0]]
		List<Interval> intervals = new ArrayList<Interval>();
		intervals.add(interval0);
		intervals.add(interval1);
		intervals.add(interval2);
		intervals.add(interval3);
		intervals.add(interval4);
		intervals.add(interval5);
		intervals.add(interval6);
		intervals.add(interval7);
		intervals.add(interval8);
		intervals.add(interval9);
		intervals.add(interval10);
		intervals.add(interval11);
		//System.out.println(intervals);
		//Collections.sort(intervals);
		intervals = merge(intervals);
			System.out.println("start merge : ");
		printIntervals(intervals);

	}

	public static void printIntervals(List<Interval> intervals) {
		//System.out.println(intervals);
		for (int i = 0; i < intervals.size(); i++) {
           System.out.println("Index: " + i + " - Item: "
			   + intervals.get(i).start + " "+ intervals.get(i).end);
		}
	}

}

/**
 * Definition for an interval. */
  class Interval {
     int start;
      int end;
      Interval() { start = 0; end = 0; }
      Interval(int s, int e) { start = s; end = e; }

  }
  
				
时间: 2024-10-13 07:47:41

2015.03.30 LeetCode Merge Intervals 解题记录的相关文章

LeetCode: Merge Intervals 解题报告

Merge IntervalsGiven 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]. SOLUTION 1: 1. 先使用Comparator 的匿名类对intervels进行排序. 2. 把Intervals遍历一次,依次一个一个merge到第1个interval. 把第1

[leetcode]Merge Intervals @ Python

原题地址:https://oj.leetcode.com/problems/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]. 解题思路:先将区间按照每个start的值来排序,排好序以后判断一个区间的start值是否处在前一个区间

LeetCode: Merge Intervals [055]

[题目] Given an array of non-negative integers, you are initially positioned at the first index of the array. 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].

[LeetCode] Merge Intervals 排序sort

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]. Show Tags Array Sort 这题其实想好思路很好解决,对于框,如果下个框开始在 其中间,则连在一起,否则单独为一个,这需要按start 排序便可以了,因为类中写自定义比较函数比较麻烦,所以一次写了好几个.

【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]. [解析] 题意:有很多个区间,把有重叠的区间合并. 思路:先排序,然后检查相邻两个区间,看前一个区间的结尾是否大于后一个区间的开始,注意前一个区间包含后一个区间的情况. 用Java自带的sort()方法,只

[LeetCode] 56. 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]. 问题:给定一个区间集合,整合所有重叠的区间. 对区间集合按照 start 来排序,然后根据 intervals[i].start 和 res.lastElement.end 来整合即可. 1 int static com

LeetCode Merge Intervals

原题链接在这里:https://leetcode.com/problems/merge-intervals/ 首先sort list 中的interval. 然后比较 前一个 interval的end是否 >= 后一个interval 的start, 若是,则合并这两个interval, 用来和下一个比较.若不是,则把这段interval 加到res中去. 通过本题见到了如何改写Comparator, 参见了这篇文章:http://www.blogjava.net/yesjoy/articles

[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]. 题意:给定一系列区间,合并重叠区间 思路:其实,个人觉得,题目的例子有一定误导性,以为区间已经按每个区间的start值排好序了.结果,毫无疑问,悲剧了.若是已经排好序,则,只需将当前区间的end值和下一个的start值比较,

LeetCode() Merge Intervals 还是有问题,留待,脑袋疼。

感觉有一点进步了,但是思路还是不够犀利. /** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0), end(0) {} * Interval(int s, int e) : start(s), end(e) {} * }; */ class Solution { public: vector<Interval> merge(vector<In