{"id":381,"date":"2020-09-26T10:34:49","date_gmt":"2020-09-26T02:34:49","guid":{"rendered":"http:\/\/39.96.58.60\/?p=381"},"modified":"2022-10-18T16:39:35","modified_gmt":"2022-10-18T08:39:35","slug":"leetcode-138-%e5%a4%8d%e5%88%b6%e5%b8%a6%e9%9a%8f%e6%9c%ba%e6%8c%87%e9%92%88%e7%9a%84%e9%93%be%e8%a1%a8","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=381","title":{"rendered":"Leetcode 138. \u590d\u5236\u5e26\u968f\u673a\u6307\u9488\u7684\u94fe\u8868"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u94fe\u8868\uff0c\u6bcf\u4e2a\u8282\u70b9\u5305\u542b\u4e00\u4e2a\u989d\u5916\u589e\u52a0\u7684\u968f\u673a\u6307\u9488\uff0c\u8be5\u6307\u9488\u53ef\u4ee5\u6307\u5411\u94fe\u8868\u4e2d\u7684\u4efb\u4f55\u8282\u70b9\u6216\u7a7a\u8282\u70b9\u3002<\/p>\n<p>\u8981\u6c42\u8fd4\u56de\u8fd9\u4e2a\u94fe\u8868\u7684&nbsp;<strong><a href=\"https:\/\/baike.baidu.com\/item\/\u6df1\u62f7\u8d1d\/22785317?fr=aladdin\" target=\"_blank\" rel=\"noopener noreferrer\">\u6df1\u62f7\u8d1d<\/a><\/strong>\u3002&nbsp;<\/p>\n<p>\u6211\u4eec\u7528\u4e00\u4e2a\u7531&nbsp;<code>n<\/code>&nbsp;\u4e2a\u8282\u70b9\u7ec4\u6210\u7684\u94fe\u8868\u6765\u8868\u793a\u8f93\u5165\/\u8f93\u51fa\u4e2d\u7684\u94fe\u8868\u3002\u6bcf\u4e2a\u8282\u70b9\u7528\u4e00\u4e2a&nbsp;<code>[val, random_index]<\/code>&nbsp;\u8868\u793a\uff1a<\/p>\n<ul>\n<li><code>val<\/code>\uff1a\u4e00\u4e2a\u8868\u793a&nbsp;<code>Node.val<\/code>&nbsp;\u7684\u6574\u6570\u3002<\/li>\n<li><code>random_index<\/code>\uff1a\u968f\u673a\u6307\u9488\u6307\u5411\u7684\u8282\u70b9\u7d22\u5f15\uff08\u8303\u56f4\u4ece&nbsp;<code>0<\/code>&nbsp;\u5230&nbsp;<code>n-1<\/code>\uff09\uff1b\u5982\u679c\u4e0d\u6307\u5411\u4efb\u4f55\u8282\u70b9\uff0c\u5219\u4e3a&nbsp;&nbsp;<code>null<\/code>&nbsp;\u3002<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<p><img alt=\"\" src=\"https:\/\/assets.leetcode-cn.com\/aliyun-lc-upload\/uploads\/2020\/01\/09\/e1.png\" style=\"height: 138px; width: 680px;\"><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>head = [[7,null],[13,0],[11,4],[10,2],[1,0]]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[[7,null],[13,0],[11,4],[10,2],[1,0]]\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<p><img alt=\"\" src=\"https:\/\/assets.leetcode-cn.com\/aliyun-lc-upload\/uploads\/2020\/01\/09\/e2.png\" style=\"height: 111px; width: 680px;\"><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>head = [[1,1],[2,1]]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[[1,1],[2,1]]\n\n<\/pre>\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n<p><strong><img alt=\"\" src=\"https:\/\/assets.leetcode-cn.com\/aliyun-lc-upload\/uploads\/2020\/01\/09\/e3.png\" style=\"height: 119px; width: 680px;\"><\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>head = [[3,null],[3,0],[3,null]]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[[3,null],[3,0],[3,null]]\n\n<\/pre>\n<p><strong>\u793a\u4f8b 4\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>head = []\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[]\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u7ed9\u5b9a\u7684\u94fe\u8868\u4e3a\u7a7a\uff08\u7a7a\u6307\u9488\uff09\uff0c\u56e0\u6b64\u8fd4\u56de null\u3002\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li><code>-10000 &lt;= Node.val &lt;= 10000<\/code><\/li>\n<li><code>Node.random<\/code>&nbsp;\u4e3a\u7a7a\uff08null\uff09\u6216\u6307\u5411\u94fe\u8868\u4e2d\u7684\u8282\u70b9\u3002<\/li>\n<li>\u8282\u70b9\u6570\u76ee\u4e0d\u8d85\u8fc7 1000 \u3002<\/li>\n<\/ul>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u54c8\u5e0c\u8868\u3001 \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\uff1a40 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8695.03% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a14.9 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e865.26% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u56de\u6eaf\n    \u4f7f\u7528head\u8bb0\u5f55\u5df2\u7ecf\u8bbf\u95ee\u8fc7\u7684\u8282\u70b9\uff0c\u548c\u65b0\u521b\u5efa\u7684\u5bf9\u5e94\u8282\u70b9\n    \u5b9e\u73b0\u8be6\u60c5\u89c1\u4ee3\u7801\u6ce8\u91ca\n\"\"\"\nclass Solution:\n    def copyRandomList(self, head: 'Node') -&gt; 'Node':\n\n        def backtrack(head, visited):\n            if head == None:    # None\u65f6\uff0c\u8fd4\u56deNone\n                return head\n\n            if head in visited: # \u5982\u679chead\u8bbf\u95ee\u8fc7\u4e86\uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u521b\u5efa\u7684\u65b0\u8282\u70b9\n                return visited[head]\n\n            else:\n                node = Node(head.val)   # \u6ca1\u6709\u8bbf\u95ee\u8fc7\uff0c\u521b\u65b0\u65b0\u8282\u70b9\n                visited[head] = node    # \u5c06\u65b0\u521b\u5efa\u7684\u8282\u70b9\uff0c\u4e0ehead\u7ed1\u5b9a\uff0c\u8bb0\u5f55\u5230\u4ee5\u8bbf\u95ee\n                node.next = backtrack(head.next, visited)   # \u8bbf\u95eehead\u8282\u70b9\u7684next\n                node.random = backtrack(head.random, visited)   # \u8bbf\u95eehead\u8282\u70b9\u7684random\n                return node\n\n        node = backtrack(head, {})\n        return node<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u94fe\u8868\uff0c\u6bcf\u4e2a\u8282\u70b9\u5305\u542b\u4e00\u4e2a\u989d\u5916\u589e\u52a0\u7684\u968f\u673a\u6307\u9488\uff0c\u8be5\u6307\u9488\u53ef\u4ee5\u6307\u5411\u94fe\u8868\u4e2d\u7684\u4efb\u4f55\u8282\u70b9\u6216\u7a7a\u8282\u70b9\u3002 \u8981\u6c42\u8fd4\u56de\u8fd9\u4e2a\u94fe\u8868\u7684&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","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\/381"}],"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=381"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/381\/revisions"}],"predecessor-version":[{"id":382,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/381\/revisions\/382"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=381"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}