【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()方法,只要自己重写compare()方法即可。

【Java代码】

/**
 * 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 class MyComparator implements Comparator<Interval> {
		@Override
		public int compare(Interval a, Interval b) {
			return a.start - b.start;
		}
    }

    public List<Interval> merge(List<Interval> intervals) {
    	List<Interval> ans = new ArrayList<Interval>();
    	if (intervals.size() == 0) return ans;

    	Collections.sort(intervals, new MyComparator());

    	int start = intervals.get(0).start;
    	int end = intervals.get(0).end;

    	for (int i = 0; i < intervals.size(); i++) {
    		Interval inter = intervals.get(i);
    		if (inter.start > end) {
    			ans.add(new Interval(start, end));
    			start = inter.start;
    			end = inter.end;
    		} else {
    			end = Math.max(end, inter.end);
    		}
    	}
    	ans.add(new Interval(start, end));

    	return ans;
    }
}
时间: 2024-08-06 18:30:39

【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

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 i

[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: Combination Sum 解题报告

Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Question Solution Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The

[LeetCode]LRU Cache, 解题报告

题目 Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.

【LeetCode】Subsets 解题报告

[题目] Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,

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