【LeetCode-面试算法经典-Java实现】【056-Merge Intervals(区间合并)】

【056-Merge Intervals(区间合并)】


【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】

原题

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

题目大意

  给定一个区间集合,合并有重叠的区间。

解题思路

  先对区间进行排序。按開始点进行排序,再一个一个进行合并。

代码实现

算法实现类

import java.util.*;

public class Solution {

    public List<Interval> merge(List<Interval> intervals) {
        List<Interval> result = new LinkedList<>();

        if (intervals == null || intervals.size() < 1) {
            return result;
        }

        // 先对区间进行排序,使用一个匿名内部类
        Collections.sort(intervals, new Comparator<Interval>() {
            @Override
            public int compare(Interval o1, Interval o2) {
                return o1.start - o2.start;
            }
        });

        // 排序后,后一个元素(记为next)的start一定是不小于前一个(记为prev)start的,
        // 对于新加入的区间,假设next.start大于prev.end就说明这两个区间是分开的,要添
        // 加一个新的区间。否则说明next.start在[prev.start, prev.end]内。则仅仅要看
        // next.end是否是大于prev.end,假设大于就要合并区间(扩大)
        Interval prev = null;
        for (Interval item : intervals) {

            if (prev == null || prev.end < item.start) {
                result.add(item);
                prev = item;
            } else if (prev.end < item.end) {
                prev.end = item.end;
            }
        }

        return result;
    }
}

评測结果

  点击图片。鼠标不释放,拖动一段位置,释放后在新的窗体中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47120501

时间: 2024-10-14 03:18:18

【LeetCode-面试算法经典-Java实现】【056-Merge Intervals(区间合并)】的相关文章

88. Merge Sorted Array【leetcode】算法,java将两个有序数组合并到一个数组中

88. Merge Sorted Array Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The n

【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】

[139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = "leetcode", di

【LeetCode-面试算法经典-Java实现】【057-Insert Interval(插入区间)】

[057-Insert Interval(插入区间)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their star

【LeetCode-面试算法经典-Java实现】【088-Merge Sorted Array(合并排序数组)】

[088-Merge Sorted Array(合并排序数组)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: You may assume that nums1 has enough space (size that is greater or equal to m +

【LeetCode-面试算法经典-Java实现】【021-Merge Two Sorted Lists(合并两个排好序的单链表)】

[021-Merge Two Sorted Lists(合并两个排好序的单链表)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 题目大意 合并两个排序链表并返回一个新的列表.新的链表的

【LeetCode-面试算法经典-Java实现】【023-Merge k Sorted Lists(合并k个排好的的单链表)】

[023-Merge k Sorted Lists(合并k个排好的的单链表)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题目大意 合并k个排好的的单链表.分析和描述它的复杂性. 解题思路 使用小顶堆来实现,先将K个链表的头结点入堆,取堆顶元素,这个结点就是最小的,接

【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】

[107-Binary Tree Level Order Traversal II(二叉树层序遍历II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example

【LeetCode-面试算法经典-Java实现】【064-Minimum Path Sum(最小路径和)】

[064-Minimum Path Sum(最小路径和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either

【LeetCode-面试算法经典-Java实现】【066-Plus One(加一)】

[066-Plus One(加一)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. 题目大意 给定一个用数组表示的一个数,