{"id":2286,"date":"2022-01-23T10:43:00","date_gmt":"2022-01-23T02:43:00","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2286"},"modified":"2022-01-23T10:43:02","modified_gmt":"2022-01-23T02:43:02","slug":"leetcodeday57","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/23\/leetcodeday57\/","title":{"rendered":"leetcodeday57 &#8211;\u63d2\u5165\u533a\u95f4"},"content":{"rendered":"\n<p>\u7ed9\u4f60\u4e00\u4e2a<strong>&nbsp;\u65e0\u91cd\u53e0\u7684<\/strong><em>&nbsp;\uff0c<\/em>\u6309\u7167\u533a\u95f4\u8d77\u59cb\u7aef\u70b9\u6392\u5e8f\u7684\u533a\u95f4\u5217\u8868\u3002<\/p>\n\n\n\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\n\n\n<p><strong>\u793a\u4f8b&nbsp;1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>intervals = [[1,3],[6,9]], newInterval = [2,5]\n<strong>\u8f93\u51fa\uff1a<\/strong>[[1,5],[6,9]]\n<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]\n<strong>\u8f93\u51fa\uff1a<\/strong>[[1,2],[3,10],[12,16]]\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<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>intervals = [], newInterval = [5,7]\n<strong>\u8f93\u51fa\uff1a<\/strong>[[5,7]]\n<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 4\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>intervals = [[1,5]], newInterval = [2,3]\n<strong>\u8f93\u51fa\uff1a<\/strong>[[1,5]]\n<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 5\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>intervals = [[1,5]], newInterval = [2,7]\n<strong>\u8f93\u51fa\uff1a<\/strong>[[1,7]]\n<\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul><li><code>0 &lt;= intervals.length &lt;= 10<sup>4<\/sup><\/code><\/li><li><code>intervals[i].length == 2<\/code><\/li><li><code>0 &lt;=&nbsp;intervals[i][0] &lt;=&nbsp;intervals[i][1] &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>intervals<\/code>&nbsp;\u6839\u636e&nbsp;<code>intervals[i][0]<\/code>&nbsp;\u6309&nbsp;<strong>\u5347\u5e8f<\/strong>&nbsp;\u6392\u5217<\/li><li><code>newInterval.length == 2<\/code><\/li><li><code>0 &lt;=&nbsp;newInterval[0] &lt;=&nbsp;newInterval[1] &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<p>\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\r\n# &#91;57] \u63d2\u5165\u533a\u95f4\r\n#\r\n\r\n# @lc code=start\r\nclass Solution:\r\n    def insert(self, intervals: List&#91;List&#91;int]], newInterval: List&#91;int]) -> List&#91;List&#91;int]]:\r\n        intervals.append(newInterval)\r\n        import copy\r\n        x=intervals\r\n        y=copy.deepcopy(intervals) \r\n        lens=len(y)\r\n        x.sort(key=lambda x: (x&#91;0], -x&#91;1]))\r\n        y.sort(key=lambda x: (x&#91;1], -x&#91;0]))\r\n        #print(x,y)\r\n\r\n        i=x.index(newInterval)\r\n        j=y.index(newInterval)\r\n        if i>0 and x&#91;i]&#91;0]&lt;=x&#91;i-1]&#91;1]:\r\n            i=i-1\r\n        if j&lt;lens-1 and y&#91;j]&#91;1]>=y&#91;j+1]&#91;0]:\r\n            j=j+1\r\n        #print(i)\r\n        return x&#91;0:i]+&#91;&#91;x&#91;i]&#91;0],y&#91;j]&#91;1]]]+y&#91;j+1:]<\/code><\/pre>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-271.png\" alt=\"\" class=\"wp-image-2291\" width=\"515\" height=\"196\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-271.png 719w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-271-300x115.png 300w\" sizes=\"(max-width: 515px) 100vw, 515px\" \/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a&nbsp;\u65e0\u91cd\u53e0\u7684&nbsp;\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 &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/23\/leetcodeday57\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcodeday57 &#8211;\u63d2\u5165\u533a\u95f4<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2286"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=2286"}],"version-history":[{"count":5,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2286\/revisions"}],"predecessor-version":[{"id":2293,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2286\/revisions\/2293"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2286"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}