{
  "type": "code_options",
  "author": "https://github.com/qiyuangong/leetcode",
  "source": "solution.md",
  "exercise_id": "4091d8bdf76f4dde9c2d6aacf65da8cd",
  "keywords": "数组,排序",
  "title": "合并区间",
  "desc": [
    {
      "content": "\n<p>以数组 <code>intervals</code> 表示若干个区间的集合，其中单个区间为 <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> 。请你合并所有重叠的区间，并返回一个不重叠的区间数组，该数组需恰好覆盖输入中的所有区间。</p><p> </p><p><strong>示例 1：</strong></p><pre><strong>输入：</strong>intervals = [[1,3],[2,6],[8,10],[15,18]]<strong><br />输出：</strong>[[1,6],[8,10],[15,18]]<strong><br />解释：</strong>区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>intervals = [[1,4],[4,5]]<strong><br />输出：</strong>[[1,5]]<strong><br />解释：</strong>区间 [1,4] 和 [4,5] 可被视为重叠区间。</pre><p> </p><p><strong>提示：</strong></p><ul>\t<li><code>1 <= intervals.length <= 10<sup>4</sup></code></li>\t<li><code>intervals[i].length == 2</code></li>\t<li><code>0 <= start<sub>i</sub> <= end<sub>i</sub> <= 10<sup>4</sup></code></li></ul>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "python"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ]
  ],
  "template": {
    "content": "class Interval(object):\n\tdef __init__(self, s=0, e=0):\n\t\tself.start = s\n\t\tself.end = e\nclass Solution(object):\n\tdef list2interval(self, list_interval):\n\t\tret = []\n\t\tfor i in list_interval:\n\t\t\tinterval = Interval(i[0], i[1])\n\t\t\tret.append(interval)\n\t\treturn ret\n\tdef interval2list(self, interval):\n\t\tret = []\n\t\tx = [0,0]\n\t\tfor i in interval:\n\t\t\tx[0] = i.start\n\t\t\tx[1] = i.end\n\t\t\tret.append(x)\n\t\t\tx = [0,0]\n\t\treturn ret\n\tdef merge(self, intervals):\n\t\t\"\"\"\n\t\t:type intervals: List[Interval]\n\t\t:rtype: List[Interval]\n\t\t\"\"\"\n\t\tif intervals is None:\n\t\t\treturn\n\t\tls = len(intervals)\n\t\tif ls <= 1:\n\t\t\treturn intervals\n\t\tintervals = self.list2interval(intervals)\t\t\n\t\tintervals.sort(key=lambda x: x.start)\n\t\tpos = 0\n\t\twhile pos < len(intervals) - 1:\n\t\t\tif intervals[pos].end >= intervals[pos + 1].start:\n\t\t\t\tnext = intervals.pop(pos + 1)\n\t\t\t\tif next.end > intervals[pos].end:\n\t\t\t\t\tintervals[pos].end = next.end\n\t\t\telse:\n\t\t\t\tpos += 1\n\t\tintervals = self.interval2list(intervals)\n\t\treturn intervals\nif __name__ == '__main__':\n\ts = Solution()\n\tprint (s.merge(intervals = [[1,4],[4,5]]))",
    "language": "python"
  },
  "node_id": "dailycode-71504d078d39443c801ae99bf0d81fa1",
  "license": "csdn.net",
  "created_at": 1637894159,
  "topic_link": "https://bbs.csdn.net/topics/600471018"
}