{"id":637,"date":"2020-09-26T11:05:59","date_gmt":"2020-09-26T03:05:59","guid":{"rendered":"http:\/\/39.96.58.60\/?p=637"},"modified":"2022-10-18T16:38:35","modified_gmt":"2022-10-18T08:38:35","slug":"leetcode-1171-%e4%bb%8e%e9%93%be%e8%a1%a8%e4%b8%ad%e5%88%a0%e5%8e%bb%e6%80%bb%e5%92%8c%e5%80%bc%e4%b8%ba%e9%9b%b6%e7%9a%84%e8%bf%9e%e7%bb%ad%e8%8a%82%e7%82%b9","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=637","title":{"rendered":"Leetcode 1171. \u4ece\u94fe\u8868\u4e2d\u5220\u53bb\u603b\u548c\u503c\u4e3a\u96f6\u7684\u8fde\u7eed\u8282\u70b9"},"content":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u94fe\u8868\u7684\u5934\u8282\u70b9&nbsp;<code>head<\/code>\uff0c\u8bf7\u4f60\u7f16\u5199\u4ee3\u7801\uff0c\u53cd\u590d\u5220\u53bb\u94fe\u8868\u4e2d\u7531 <strong>\u603b\u548c<\/strong>&nbsp;\u503c\u4e3a <code>0<\/code> \u7684\u8fde\u7eed\u8282\u70b9\u7ec4\u6210\u7684\u5e8f\u5217\uff0c\u76f4\u5230\u4e0d\u5b58\u5728\u8fd9\u6837\u7684\u5e8f\u5217\u4e3a\u6b62\u3002<\/p>\n<p>\u5220\u9664\u5b8c\u6bd5\u540e\uff0c\u8bf7\u4f60\u8fd4\u56de\u6700\u7ec8\u7ed3\u679c\u94fe\u8868\u7684\u5934\u8282\u70b9\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u4f60\u53ef\u4ee5\u8fd4\u56de\u4efb\u4f55\u6ee1\u8db3\u9898\u76ee\u8981\u6c42\u7684\u7b54\u6848\u3002<\/p>\n<p>\uff08\u6ce8\u610f\uff0c\u4e0b\u9762\u793a\u4f8b\u4e2d\u7684\u6240\u6709\u5e8f\u5217\uff0c\u90fd\u662f\u5bf9&nbsp;<code>ListNode<\/code>&nbsp;\u5bf9\u8c61\u5e8f\u5217\u5316\u7684\u8868\u793a\u3002\uff09<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>head = [1,2,-3,3,1]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[3,1]\n\n<strong>\u63d0\u793a\uff1a<\/strong>\u7b54\u6848 [1,2,1] \u4e5f\u662f\u6b63\u786e\u7684\u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>head = [1,2,3,-3,4]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[1,2,4]\n\n<\/pre>\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>head = [1,2,3,-3,-2]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[1]\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li>\u7ed9\u4f60\u7684\u94fe\u8868\u4e2d\u53ef\u80fd\u6709 <code>1<\/code> \u5230&nbsp;<code>1000<\/code>&nbsp;\u4e2a\u8282\u70b9\u3002<\/li>\n<li>\u5bf9\u4e8e\u94fe\u8868\u4e2d\u7684\u6bcf\u4e2a\u8282\u70b9\uff0c\u8282\u70b9\u7684\u503c\uff1a<code>-1000 &lt;= node.val &lt;= 1000<\/code>.<\/li>\n<\/ul>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u94fe\u8868\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\uff1a48 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8695.06% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a14.4 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e865.35% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    i~j \u548c\u4e3a0\uff0c \u5219\u5fc5\u7136 sum(0~i) == sum(0~j)\n    \u4e14\uff0c\u6bcf\u6b21\u5220\u9664\u540e\uff0c\u9700\u8981\u91cd\u65b0\u4ece\u5934\u8ba1\u7b97\uff0c\u4e0d\u7136\u4f1a\u51fa\u73b0\u4e2d\u95f4\u5220\u53bb\u90e8\u5206\u53c8\u91cd\u590d\u5220\u53bb\u5bfc\u81f4\u7ed3\u679c\u9519\u8bef\u7684\u60c5\u51b5\n        [1, 3, 2, -3, -2, 5, 5, -5, 1]\n      0  1  4  6   3   1  6  11  6  7\n\"\"\"\nclass Solution:\n    def removeZeroSumSublists(self, head: ListNode) -&gt; ListNode:\n        start = ListNode(0, next=head)\n        record = {0: start}\n        add = 0\n        while head:\n            add += head.val\n            if add in record:\n                record[add].next = head.next    # \u5220\u9664\u5b50\u94fe\u8868\n                return self.removeZeroSumSublists(start.next)   # \u5220\u53bb\u4e00\u4e2a\u5b50\u94fe\u8868\u540e\uff0c\u91cd\u65b0\u8ba1\u7b97\n            else:\n                record[add] = head\n                head = head.next\n        return start.next<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u94fe\u8868\u7684\u5934\u8282\u70b9&nbsp;head\uff0c\u8bf7\u4f60\u7f16\u5199\u4ee3\u7801\uff0c\u53cd\u590d\u5220\u53bb\u94fe\u8868\u4e2d\u7531 \u603b\u548c&nbsp;\u503c\u4e3a 0 \u7684\u8fde\u7eed\u8282\u70b9&#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\/637"}],"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=637"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/637\/revisions"}],"predecessor-version":[{"id":638,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/637\/revisions\/638"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=637"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}