{"id":451,"date":"2020-09-26T10:43:15","date_gmt":"2020-09-26T02:43:15","guid":{"rendered":"http:\/\/39.96.58.60\/?p=451"},"modified":"2022-10-18T16:39:14","modified_gmt":"2022-10-18T08:39:14","slug":"leetcode-57-%e6%8f%92%e5%85%a5%e5%8c%ba%e9%97%b4","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=451","title":{"rendered":"Leetcode 57. \u63d2\u5165\u533a\u95f4"},"content":{"rendered":"<p>\u7ed9\u51fa\u4e00\u4e2a<em>\u65e0\u91cd\u53e0\u7684 \uff0c<\/em>\u6309\u7167\u533a\u95f4\u8d77\u59cb\u7aef\u70b9\u6392\u5e8f\u7684\u533a\u95f4\u5217\u8868\u3002<\/p>\n<p>\u5728\u5217\u8868\u4e2d\u63d2\u5165\u4e00\u4e2a\u65b0\u7684\u533a\u95f4\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u5217\u8868\u4e2d\u7684\u533a\u95f4\u4ecd\u7136\u6709\u5e8f\u4e14\u4e0d\u91cd\u53e0\uff08\u5982\u679c\u6709\u5fc5\u8981\u7684\u8bdd\uff0c\u53ef\u4ee5\u5408\u5e76\u533a\u95f4\uff09\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b&nbsp;1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>intervals = [[1,3],[6,9]], newInterval = [2,5]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[[1,5],[6,9]]\n\n<\/pre>\n<p><strong>\u793a\u4f8b&nbsp;2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>intervals = <code>[[1,2],[3,5],[6,7],[8,10],[12,16]]<\/code>, newInterval = <code>[4,8]<\/code>\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[[1,2],[3,10],[12,16]]\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u8fd9\u662f\u56e0\u4e3a\u65b0\u7684\u533a\u95f4 <code>[4,8]<\/code> \u4e0e <code>[3,5],[6,7],[8,10]<\/code>&nbsp;\u91cd\u53e0\u3002\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong>\u8f93\u5165\u7c7b\u578b\u5df2\u5728 2019 \u5e74 4 \u6708 15 \u65e5\u66f4\u6539\u3002\u8bf7\u91cd\u7f6e\u4e3a\u9ed8\u8ba4\u4ee3\u7801\u5b9a\u4e49\u4ee5\u83b7\u53d6\u65b0\u7684\u65b9\u6cd5\u7b7e\u540d\u3002<\/p>\n<p>**\u96be\u5ea6**: Hard<\/p>\n<p>**\u6807\u7b7e**: \u6392\u5e8f\u3001 \u6570\u7ec4\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a40 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8693.88% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a15.2 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8610.79% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u6a21\u62df\u63d2\u5165\n\"\"\"\nclass Solution:\n    def insert(self, intervals: List[List[int]], newInterval: List[int]) -&gt; List[List[int]]:\n        result = []\n        i = 0\n        start, end = newInterval[0], newInterval[1]\n        if intervals == []: # \u5982\u679c\u8d77\u59cb\u5217\u8868\u4e3a\u7a7a\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u5f85\u63d2\u5165\u5217\u8868\n            return [newInterval]\n        while i &lt; len(intervals):\n            if intervals[i][1] &lt; start: # \u5f85\u63d2\u5165\u5217\u8868\u5728\u5f53\u524d\u5217\u8868\u5143\u7d20\u53f3\u4fa7\n                result.append(intervals[i]) # \u76f4\u63a5\u63d2\u5165\u5f53\u524d\u5143\u7d20\n            elif end &lt; intervals[i][0]: # \u5f85\u63d2\u5165\u5217\u8868\u5728\u5f53\u524d\u5217\u8868\u5143\u7d20\u5de6\u4fa7\uff0c\u5c06\u5f85\u63d2\u5165\u5217\u8868\u63d2\u5165\u7ed3\u679c\uff0c\u4ee5\u5f53\u524d\u5217\u8868\u503c\u66f4\u65b0\u5f85\u63d2\u5165\u5217\u8868\n                result.append([start, end])\n                start, end = intervals[i]\n            else:   # \u5f53\u524d\u5217\u8868\u4e0e\u5f85\u63d2\u5165\u5217\u8868\u5b58\u5728\u4ea4\u96c6\uff0c\u5219\u66f4\u65b0\u5f85\u63d2\u5165\u5217\u8868\n                start = min(start, intervals[i][0])\n                end = max(end, intervals[i][1])\n            i += 1\n        result.append([start, end]) # \u6700\u540e\u5c06\u5f85\u63d2\u5165\u5217\u8868\u63d2\u5165\u6700\u7ec8\u7ed3\u679c\n        return result<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u51fa\u4e00\u4e2a\u65e0\u91cd\u53e0\u7684 \uff0c\u6309\u7167\u533a\u95f4\u8d77\u59cb\u7aef\u70b9\u6392\u5e8f\u7684\u533a\u95f4\u5217\u8868\u3002 \u5728\u5217\u8868\u4e2d\u63d2\u5165\u4e00\u4e2a\u65b0\u7684\u533a\u95f4\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u5217\u8868\u4e2d\u7684\u533a\u95f4\u4ecd\u7136\u6709\u5e8f&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/451"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=451"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/451\/revisions"}],"predecessor-version":[{"id":452,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/451\/revisions\/452"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=451"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}