{"id":2969,"date":"2020-02-18T11:14:35","date_gmt":"2020-02-18T03:14:35","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=2969"},"modified":"2020-02-18T11:14:35","modified_gmt":"2020-02-18T03:14:35","slug":"%e3%80%90leetcode%e3%80%910056-%e5%90%88%e5%b9%b6%e5%8c%ba%e9%97%b4","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/algorithm\/2969\/","title":{"rendered":"\u3010LeetCode\u30110056&amp;0057 \u5408\u5e76\u533a\u95f4&amp;\u63d2\u5165\u533a\u95f4"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/merge-intervals\/\">https:\/\/leetcode-cn.com\/problems\/merge-intervals\/<\/a><\/p>\n\n\n<pre class=\"wp-block-preformatted\"># -*- coding:utf-8 -*-\n<em>\n<\/em>class Solution(object):\n    def merge(self, intervals):\n        <em>\"\"\"\n        <\/em><strong><em>:type<\/em><\/strong><em> intervals: List[List[int]]\n        <\/em><strong><em>:rtype<\/em><\/strong><em>: List[List[int]]\n        \"\"\"\n        <\/em>def cmp(x, y):\n            if x[0] > y[0]:\n                return 1\n            elif x[0] &lt; y[0]:\n                return -1\n            else:\n                return 0\n        if len(intervals) == 1:\n            return intervals\n        import functools\n        intervals = sorted(intervals, key=functools.cmp_to_key(cmp))\n        do_length = 0\n        do_idx = 1\n        max_length = len(intervals)\n        while do_length &lt; max_length - 1:\n            if intervals[do_idx][0] &lt;= intervals[do_idx - 1][1]:\n                intervals[do_idx - 1][1] = max(intervals[do_idx][1], intervals[do_idx - 1][1])\n                intervals.remove(intervals[do_idx])\n            else:\n                do_idx += 1\n            do_length += 1\n        return intervals\nif __name__ == '__main__':\n    intervals = [[1, 3], [2, 6], [8, 10], [15, 18]]\n    intervals = [[1, 3], [5, 6]]\n    # intervals = [[1, 4], [0, 2], [3, 5]]\n    print(Solution().merge(intervals))\n<\/pre>\n\n\n<p><strong>\u601d\u8def<\/strong>\uff1a\u9996\u5148\u662f\u6392\u5e8f\uff0c\u7136\u540e\u6839\u636e\u5f53\u524d\u76840\u53f7\u4f4d\u662f\u5426\u5c0f\u4e8e\u524d\u4e00\u4e2a\u76841\u53f7\u4f4d\u6765\u5224\u65ad\u8fd9\u4e24\u4e2a\u662f\u5426\u53ef\u4ee5\u5408\u5e76\uff0c\u5982\u679c\u53ef\u4ee5\u5c31\u5408\u5e76\u3002<\/p>\n\n\n<p>\u8fd9\u91cc\u4e86\u89e3\u4e86python\u7684\u81ea\u5b9a\u4e49\u6bd4\u8f83\u51fd\u6570\uff0c\u8fd8\u5fc5\u987b\u5bfc\u5165\u4e00\u4e2afuntools\u3002\u3002\u5b66\u5230\u4e86\u5b66\u5230\u4e86\u3002<\/p>\n\n\n<p><\/p>\n\n\n<p>\u63d2\u5165\u533a\u95f4\u53ea\u8981\u5728\u4e0a\u9762\u4ee3\u7801\u4e0a\u505a\u4e00\u4e9b\u5fae\u5c0f\u7684\u6539\u52a8\uff0c\u5c06newInterval\u63d2\u5165intervals\u7136\u540e\u6539\u4e00\u4e0b\u8fb9\u754c\u6761\u4ef6\u5373\u53ef\u3002<\/p>\n\n\n<pre class=\"wp-block-preformatted\"># -*- coding:utf-8 -*-\n<em>\n<\/em>class Solution(object):\n    def insert(self, intervals, newInterval):\n        <em>\"\"\"\n        <\/em><strong><em>:type<\/em><\/strong><em> intervals: List[List[int]]\n        <\/em><strong><em>:type<\/em><\/strong><em> newInterval: List[int]\n        <\/em><strong><em>:rtype<\/em><\/strong><em>: List[List[int]]\n        \"\"\"\n        <\/em>def cmp(x, y):\n            if x[0] > y[0]:\n                return 1\n            elif x[0] &lt; y[0]:\n                return -1\n            else:\n                return 0\n        if len(intervals) == 0:\n            return [newInterval]\n        import functools\n        intervals.append(newInterval)\n        intervals = sorted(intervals, key=functools.cmp_to_key(cmp))\n        do_length = 0\n        do_idx = 1\n        max_length = len(intervals)\n        while do_length &lt; max_length - 1:\n            if intervals[do_idx][0] &lt;= intervals[do_idx - 1][1]:\n                intervals[do_idx - 1][1] = max(intervals[do_idx][1], intervals[do_idx - 1][1])\n                intervals.remove(intervals[do_idx])\n            else:\n                do_idx += 1\n            do_length += 1\n        return intervals\nif __name__ == '__main__':\n    intervals = []\n    newInterval = [5, 7]\n    print(Solution().insert(intervals, newInterval))\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode-cn.com\/problems\/merge-intervals\/  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[10],"tags":[],"views":1456,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2969"}],"collection":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2969"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2969\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2969"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}