[LeetCode] 56. Merge Intervals 合併區間 LeetCode All in One 題目講解彙總(持續更新中...)

 

Given a collection of intervals, merge all overlapping intervals.html

Example 1:git

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, merge them into [1,6].

Example 2:github

Input: [[1,4],[4,5]]
Output: [[1,5]]
Explanation: Intervals [1,4] and [4,5] are considered overlapping.

NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.數組

 

這道和以前那道 Insert Interval 很相似,此次題目要求咱們合併區間,以前那題明確了輸入區間集是有序的,而這題沒有,因此咱們首先要作的就是給區間集排序,因爲咱們要排序的是個結構體,因此咱們要定義本身的 comparator,才能用 sort 來排序,咱們以 start 的值從小到大來排序,排完序咱們就能夠開始合併了,首先把第一個區間存入結果中,而後從第二個開始遍歷區間集,若是結果中最後一個區間和遍歷的當前區間無重疊,直接將當前區間存入結果中,若是有重疊,將結果中最後一個區間的 end 值更新爲結果中最後一個區間的 end 和當前 end 值之中的較大值,而後繼續遍歷區間集,以此類推能夠獲得最終結果,代碼以下:app

 

解法一:ide

class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        if (intervals.empty()) return {};
        sort(intervals.begin(), intervals.end());
        vector<vector<int>> res{intervals[0]};
        for (int i = 1; i < intervals.size(); ++i) {
            if (res.back()[1] < intervals[i][0]) {
                res.push_back(intervals[i]);
            } else {
                res.back()[1] = max(res.back()[1], intervals[i][1]);
            }
        }   
        return res;
    }
};

 

下面這種解法將起始位置和結束位置分別存到了兩個不一樣的數組 starts 和 ends 中,而後分別進行排序,以後用兩個指針i和j,初始化時分別指向 starts 和 ends 數組的首位置,而後若是i指向 starts 數組中的最後一個位置,或者當 starts 數組上 i+1 位置上的數字大於 ends 數組的i位置上的數時,此時說明區間已經不連續了,咱們來看題目中的例子,排序後的 starts 和 ends 爲:函數

starts:    1    2    8    15post

ends:     3    6    10    18url

紅色爲i的位置,藍色爲j的位置,那麼此時 starts[i+1] 爲8,ends[i] 爲6,8大於6,因此此時不連續了,將區間 [starts[j], ends[i]],即 [1, 6] 加入結果 res 中,而後j賦值爲 i+1 繼續循環,參見代碼以下:spa

 

解法二:

class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        int n = intervals.size();
        vector<vector<int>> res;
        vector<int> starts, ends;
        for (int i = 0; i < n; ++i) {
            starts.push_back(intervals[i][0]);
            ends.push_back(intervals[i][1]);
        }
        sort(starts.begin(), starts.end());
        sort(ends.begin(), ends.end());
        for (int i = 0, j = 0; i < n; ++i) {
            if (i == n - 1 || starts[i + 1] > ends[i]) {
                res.push_back({starts[j], ends[i]});
                j = i + 1;
            }
        } 
        return res;
    }
};

 

這道題還有另外一種解法,這個解法直接調用了以前那道題 Insert Interval 的函數,因爲插入的過程當中也有合併的操做,因此咱們能夠創建一個空的集合,而後把區間集的每個區間當作一個新的區間插入結果中,也能夠獲得合併後的結果,那道題中的四種解法均可以在這裏使用,可是不必都列出來,這裏只選了那道題中的解法二放到這裏,代碼以下:

 

解法三:

class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        vector<vector<int>> res;
        for (int i = 0; i < intervals.size(); ++i) {
            res = insert(res, intervals[i]);
        }
        return res;
    }
    vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int> newInterval) {
        vector<vector<int>> res;
        int n = intervals.size(), cur = 0;
        for (int i = 0; i < n; ++i) {
            if (intervals[i][1] < newInterval[0]) {
                res.push_back(intervals[i]);
                ++cur;
            } else if (intervals[i][0] > newInterval[1]) {
                res.push_back(intervals[i]);
            } else {
                newInterval[0] = min(newInterval[0], intervals[i][0]);
                newInterval[1] = max(newInterval[1], intervals[i][1]);
            }
        }
        res.insert(res.begin() + cur, newInterval);
        return res;
    }
};

 

Github 同步地址:

https://github.com/grandyang/leetcode/issues/56

 

相似題目:

Employee Free Time

Insert Interval

Meeting Rooms II

Meeting Rooms

Teemo Attacking

Add Bold Tag in String

Range Module

Partition Labels

Interval List Intersections

 

參考資料:

https://leetcode.com/problems/merge-intervals/

https://leetcode.com/problems/merge-intervals/discuss/21242/C++-10-line-solution.-easing-understanding

https://leetcode.com/problems/merge-intervals/discuss/21223/Beat-98-Java.-Sort-start-and-end-respectively

 

LeetCode All in One 題目講解彙總(持續更新中...)

相關文章
相關標籤/搜索