56. Merge Intervals - LeetCode

Question

56. Merge Intervals

Solution

题目大意:

一个坐标轴,给你n个范围,把重叠的范围合并,返回合并后的坐标对

思路:

先排序,再遍历判断下一个开始是否在上一个范围内,如果在且结束坐标大于上一个坐标就合并

Java实现:

public List<Interval> merge(List<Interval> intervals) {
    if (intervals == null || intervals.size() == 0) return intervals;

    // sort
    intervals = intervals.stream()
        .sorted(Comparator.comparing(e -> e.start))
        .collect(Collectors.toList());

    List<Interval> result = new ArrayList<>();
    int start = intervals.get(0).start;
    int end = intervals.get(0).end;
    for (int i = 1; i < intervals.size(); i++) {
        Interval tmp = intervals.get(i);
        if (tmp.start >= start && tmp.start <= end) {
            if (tmp.end > end) end = tmp.end;
        } else {
            result.add(new Interval(start, end));
            start = tmp.start;
            end = tmp.end;
        }
    }
    result.add(new Interval(start, end));
    return result;
}

原文地址:https://www.cnblogs.com/okokabcd/p/9610368.html

时间: 2024-11-07 17:01:58

56. Merge Intervals - LeetCode的相关文章

LeetCode --- 56. Merge Intervals

题目链接: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]. 这道题的要求是将给定的一组间隔中有重叠的进行合并. 将间隔合并,首先要找到相邻的间隔,然后看其是否有重叠,如果有,就进行合并. 因此,首先考虑对数组排序.排序的时候,只需要按

LeetCode开心刷题四十二天——56. Merge Intervals

56. Merge Intervals Medium 2509194FavoriteShare Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]] Explanation: Since intervals [1,3] and [2,6] overlaps, mer

No.56 Merge Intervals

No.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]. 法一:直接复用之前写的insert函数,依次将区间段插入到结果集中 1 #include "stdafx.h" 2 #include <vector> 3

[Leetcode][Python]56: Merge Intervals

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 56: Merge Intervalshttps://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]

leetCode 56.Merge Intervals (合并区间) 解题思路和方法

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. * publi

56. Merge Intervals 57. Insert Interval *HARD*

1. Merge 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. * struct Interval { * int start; * int end; * Interval() : start(0)

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]. 题目标签:Array 这道题目给了我们一个区间的list,让我们返回一个list,是合并了所有有重叠的区间之后的list.这道题目的关键在于如何判断两个区间有重叠,根据原题给的例子可以看出,在按照每个区间的start排序

【一天一道LeetCode】#56. Merge Intervals

一天一道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]. (二)解题 /* 本题的解题步骤: 1.对vector里面的每个Interval结构体按照start的值升序排列 2.遍历整个intervals,如果有重复区域就合并,如果没

【leetcode】56. Merge Intervals

@requires_authorization @create_time 2015.7.10 11:07 @author johnsondu @url https://leetcode.com/problems/merge-intervals/ /** * 进行数组排序,然后按照贪心策略进行合并,具体是考察 * 下一个元素的start是否是介于当前的start和end之间, * 如果是,则根据需要更新当前end. * 时间复杂度(O(nlogn))-->排序, 结果获取O(n) * 空间复杂度(