# 0056. 合并区间 ## 题目地址(56. 合并区间) <https://leetcode-cn.com/problems/merge-intervals/> ## 题目描述 ``` <pre class="calibre18">``` 给出一个区间的集合,请合并所有重叠的区间。 示例 1: 输入: intervals = [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: 区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6]. 示例 2: 输入: intervals = [[1,4],[4,5]] 输出: [[1,5]] 解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间。 注意:输入类型已于2019年4月15日更改。 请重置默认代码定义以获取新方法签名。 提示: intervals[i][0] <= intervals[i][1] ``` ``` ## 前置知识 - 排序 ## 公司 - 阿里 - 腾讯 - 百度 - 字节 ## 思路 - 先对数组进行排序,排序的依据就是每一项的第一个元素的大小。 - 然后我们对数组进行遍历,遍历的时候两两运算(具体运算逻辑见下) - 判断是否相交,如果不相交,则跳过 - 如果相交,则合并两项 ## 关键点解析 - 对数组进行排序简化操作 - 如果不排序,需要借助一些 hack,这里不介绍了 ## 代码 - 语言支持: Javascript,Python3 ``` <pre class="calibre18">``` <span class="hljs-title">/* * @lc app=leetcode id=56 lang=javascript * * [56] Merge Intervals */</span> <span class="hljs-title">/** * @param {number[][]} intervals * @return {number[][]} */</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">intersected</span>(<span class="hljs-params">a, b</span>) </span>{ <span class="hljs-keyword">if</span> (a[<span class="hljs-params">0</span>] > b[<span class="hljs-params">1</span>] || a[<span class="hljs-params">1</span>] < b[<span class="hljs-params">0</span>]) <span class="hljs-keyword">return</span> <span class="hljs-params">false</span>; <span class="hljs-keyword">return</span> <span class="hljs-params">true</span>; } <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">mergeTwo</span>(<span class="hljs-params">a, b</span>) </span>{ <span class="hljs-keyword">return</span> [<span class="hljs-params">Math</span>.min(a[<span class="hljs-params">0</span>], b[<span class="hljs-params">0</span>]), <span class="hljs-params">Math</span>.max(a[<span class="hljs-params">1</span>], b[<span class="hljs-params">1</span>])]; } <span class="hljs-keyword">var</span> merge = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">intervals</span>) </span>{ <span class="hljs-title">// 这种算法需要先排序</span> intervals.sort((a, b) => a[<span class="hljs-params">0</span>] - b[<span class="hljs-params">0</span>]); <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-params">0</span>; i < intervals.length - <span class="hljs-params">1</span>; i++) { <span class="hljs-keyword">const</span> cur = intervals[i]; <span class="hljs-keyword">const</span> next = intervals[i + <span class="hljs-params">1</span>]; <span class="hljs-keyword">if</span> (intersected(cur, next)) { intervals[i] = <span class="hljs-params">undefined</span>; intervals[i + <span class="hljs-params">1</span>] = mergeTwo(cur, next); } } <span class="hljs-keyword">return</span> intervals.filter((q) => q); }; ``` ``` Python3 Code: ``` <pre class="calibre18">``` <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span>:</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">merge</span><span class="hljs-params">(self, intervals: List[List[int]])</span> -> List[List[int]]:</span> <span class="hljs-string">"""先排序,后合并"""</span> <span class="hljs-keyword">if</span> len(intervals) <= <span class="hljs-params">1</span>: <span class="hljs-keyword">return</span> intervals <span class="hljs-title"># 排序</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_first</span><span class="hljs-params">(a_list)</span>:</span> <span class="hljs-keyword">return</span> a_list[<span class="hljs-params">0</span>] intervals.sort(key=get_first) <span class="hljs-title"># 合并</span> res = [intervals[<span class="hljs-params">0</span>]] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-params">1</span>, len(intervals)): <span class="hljs-keyword">if</span> intervals[i][<span class="hljs-params">0</span>] <= res[<span class="hljs-params">-1</span>][<span class="hljs-params">1</span>]: res[<span class="hljs-params">-1</span>] = [res[<span class="hljs-params">-1</span>][<span class="hljs-params">0</span>], max(res[<span class="hljs-params">-1</span>][<span class="hljs-params">1</span>], intervals[i][<span class="hljs-params">1</span>])] <span class="hljs-keyword">else</span>: res.append(intervals[i]) <span class="hljs-keyword">return</span> res ``` ``` ***复杂度分析*** - 时间复杂度:由于采用了排序,因此复杂度大概为 O(NlogN)O(NlogN)O(NlogN) - 空间复杂度:O(N)O(N)O(N)