{"id":2625,"date":"2022-02-06T11:32:29","date_gmt":"2022-02-06T03:32:29","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2625"},"modified":"2022-02-06T11:32:30","modified_gmt":"2022-02-06T03:32:30","slug":"leetcodeday138","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/02\/06\/leetcodeday138\/","title":{"rendered":"leetcodeday138&#8211;\u590d\u5236\u5e26\u968f\u673a\u6307\u9488\u7684\u94fe\u8868"},"content":{"rendered":"\n<p>\u7ed9\u4f60\u4e00\u4e2a\u957f\u5ea6\u4e3a&nbsp;<code>n<\/code>&nbsp;\u7684\u94fe\u8868\uff0c\u6bcf\u4e2a\u8282\u70b9\u5305\u542b\u4e00\u4e2a\u989d\u5916\u589e\u52a0\u7684\u968f\u673a\u6307\u9488&nbsp;<code>random<\/code>&nbsp;\uff0c\u8be5\u6307\u9488\u53ef\u4ee5\u6307\u5411\u94fe\u8868\u4e2d\u7684\u4efb\u4f55\u8282\u70b9\u6216\u7a7a\u8282\u70b9\u3002<\/p>\n\n\n\n<p>\u6784\u9020\u8fd9\u4e2a\u94fe\u8868\u7684&nbsp;<strong><a href=\"https:\/\/baike.baidu.com\/item\/%E6%B7%B1%E6%8B%B7%E8%B4%9D\/22785317?fr=aladdin\" target=\"_blank\" rel=\"noreferrer noopener\">\u6df1\u62f7\u8d1d<\/a><\/strong>\u3002&nbsp;\u6df1\u62f7\u8d1d\u5e94\u8be5\u6b63\u597d\u7531&nbsp;<code>n<\/code>&nbsp;\u4e2a&nbsp;<strong>\u5168\u65b0<\/strong>&nbsp;\u8282\u70b9\u7ec4\u6210\uff0c\u5176\u4e2d\u6bcf\u4e2a\u65b0\u8282\u70b9\u7684\u503c\u90fd\u8bbe\u4e3a\u5176\u5bf9\u5e94\u7684\u539f\u8282\u70b9\u7684\u503c\u3002\u65b0\u8282\u70b9\u7684&nbsp;<code>next<\/code>&nbsp;\u6307\u9488\u548c&nbsp;<code>random<\/code>&nbsp;\u6307\u9488\u4e5f\u90fd\u5e94\u6307\u5411\u590d\u5236\u94fe\u8868\u4e2d\u7684\u65b0\u8282\u70b9\uff0c\u5e76\u4f7f\u539f\u94fe\u8868\u548c\u590d\u5236\u94fe\u8868\u4e2d\u7684\u8fd9\u4e9b\u6307\u9488\u80fd\u591f\u8868\u793a\u76f8\u540c\u7684\u94fe\u8868\u72b6\u6001\u3002<strong>\u590d\u5236\u94fe\u8868\u4e2d\u7684\u6307\u9488\u90fd\u4e0d\u5e94\u6307\u5411\u539f\u94fe\u8868\u4e2d\u7684\u8282\u70b9&nbsp;<\/strong>\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u5982\u679c\u539f\u94fe\u8868\u4e2d\u6709&nbsp;<code>X<\/code>&nbsp;\u548c&nbsp;<code>Y<\/code>&nbsp;\u4e24\u4e2a\u8282\u70b9\uff0c\u5176\u4e2d&nbsp;<code>X.random --&gt; Y<\/code>&nbsp;\u3002\u90a3\u4e48\u5728\u590d\u5236\u94fe\u8868\u4e2d\u5bf9\u5e94\u7684\u4e24\u4e2a\u8282\u70b9&nbsp;<code>x<\/code>&nbsp;\u548c&nbsp;<code>y<\/code>&nbsp;\uff0c\u540c\u6837\u6709&nbsp;<code>x.random --&gt; y<\/code>&nbsp;\u3002<\/p>\n\n\n\n<p>\u8fd4\u56de\u590d\u5236\u94fe\u8868\u7684\u5934\u8282\u70b9\u3002<\/p>\n\n\n\n<p>\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\n\n\n<ul><li><code>val<\/code>\uff1a\u4e00\u4e2a\u8868\u793a&nbsp;<code>Node.val<\/code>&nbsp;\u7684\u6574\u6570\u3002<\/li><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><\/ul>\n\n\n\n<p>\u4f60\u7684\u4ee3\u7801&nbsp;<strong>\u53ea<\/strong>&nbsp;\u63a5\u53d7\u539f\u94fe\u8868\u7684\u5934\u8282\u70b9&nbsp;<code>head<\/code>&nbsp;\u4f5c\u4e3a\u4f20\u5165\u53c2\u6570\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img src=\"https:\/\/assets.leetcode-cn.com\/aliyun-lc-upload\/uploads\/2020\/01\/09\/e1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>head = &#91;&#91;7,null],&#91;13,0],&#91;11,4],&#91;10,2],&#91;1,0]]\n<strong>\u8f93\u51fa\uff1a<\/strong>&#91;&#91;7,null],&#91;13,0],&#91;11,4],&#91;10,2],&#91;1,0]]<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img src=\"https:\/\/assets.leetcode-cn.com\/aliyun-lc-upload\/uploads\/2020\/01\/09\/e2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>head = &#91;&#91;1,1],&#91;2,1]]\n<strong>\u8f93\u51fa\uff1a<\/strong>&#91;&#91;1,1],&#91;2,1]]<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img src=\"https:\/\/assets.leetcode-cn.com\/aliyun-lc-upload\/uploads\/2020\/01\/09\/e3.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165\uff1a<\/strong>head = &#91;&#91;3,null],&#91;3,0],&#91;3,null]]\n<strong>\u8f93\u51fa\uff1a<\/strong>&#91;&#91;3,null],&#91;3,0],&#91;3,null]]<\/code><\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul><li><code>0 &lt;= n &lt;= 1000<\/code><\/li><li><code>-10<sup>4<\/sup>&nbsp;&lt;= Node.val &lt;= 10<sup>4<\/sup><\/code><\/li><li><code>Node.random<\/code>&nbsp;\u4e3a&nbsp;<code>null<\/code>&nbsp;\u6216\u6307\u5411\u94fe\u8868\u4e2d\u7684\u8282\u70b9\u3002<\/li><\/ul>\n\n\n\n<p>\u5173\u952e:\u9700\u8981\u4e00\u4e2a\u54c8\u5e0c\u8868\u8bb0\u5f55\u5df2\u7ecf\u8bbf\u95ee\u8fc7\u7684\u8282\u70b9\u3002\u5982\u679c \u8282\u70b9\u8bbf\u95ee\u8fc7\uff0c\u5219\u4e0d\u7528\u5165\u961f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># @lc app=leetcode.cn id=138 lang=python3\r\n#\r\n# &#91;138] \u590d\u5236\u5e26\u968f\u673a\u6307\u9488\u7684\u94fe\u8868\r\n#\r\n\r\n# @lc code=start\r\n\"\"\"\r\n# Definition for a Node.\r\nclass Node:\r\n    def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None):\r\n        self.val = int(x)\r\n        self.next = next\r\n        self.random = random\r\n\"\"\"\r\n\r\nfrom collections import deque\r\nclass Solution:\r\n    def copyRandomList(self, head: 'Optional&#91;Node]') -> 'Optional&#91;Node]':\r\n        \r\n        if not head:\r\n            return head\r\n\r\n        visited = {}\r\n\r\n        # \u5c06\u9898\u76ee\u7ed9\u5b9a\u7684\u8282\u70b9\u6dfb\u52a0\u5230\u961f\u5217\r\n        queue = deque(&#91;head])\r\n        # \u514b\u9686\u7b2c\u4e00\u4e2a\u8282\u70b9\u5e76\u5b58\u50a8\u5230\u54c8\u5e0c\u8868\u4e2d\r\n        visited&#91;head] = Node(head.val)\r\n\r\n        # \u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\r\n        while queue:\r\n            # \u53d6\u51fa\u961f\u5217\u7684\u5934\u8282\u70b9\r\n            n = queue.popleft()\r\n            # \u904d\u5386\u8be5\u8282\u70b9\u7684\u90bb\u5c45\r\n            neighbors=&#91;n.next,n.random] \r\n            i=0\r\n            for neighbor in neighbors:\r\n                \r\n                if  neighbor!=None and neighbor not in visited:\r\n                    # \u5982\u679c\u6ca1\u6709\u88ab\u8bbf\u95ee\u8fc7\uff0c\u5c31\u514b\u9686\u5e76\u5b58\u50a8\u5728\u54c8\u5e0c\u8868\u4e2d\r\n                    visited&#91;neighbor] = Node(neighbor.val)\r\n                    # \u5c06\u90bb\u5c45\u8282\u70b9\u52a0\u5165\u961f\u5217\u4e2d\r\n                    queue.append(neighbor)\r\n                    # \u66f4\u65b0\u5f53\u524d\u8282\u70b9\u7684\u90bb\u5c45\u5217\u8868\r\n                if neighbor!=None and i==0:\r\n                    visited&#91;n].next=visited&#91;neighbor]\r\n                if neighbor!=None and i==1:\r\n                    visited&#91;n].random=visited&#91;neighbor]\r\n                i=i+1\r\n\r\n\r\n        return visited&#91;head]\r\n# @lc code=end<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"767\" height=\"232\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-42.png\" alt=\"\" class=\"wp-image-2626\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-42.png 767w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/02\/image-42-300x91.png 300w\" sizes=\"(max-width: 767px) 100vw, 767px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u957f\u5ea6\u4e3a&nbsp;n&nbsp;\u7684\u94fe\u8868\uff0c\u6bcf\u4e2a\u8282\u70b9\u5305\u542b\u4e00\u4e2a\u989d\u5916\u589e\u52a0\u7684\u968f\u673a\u6307\u9488&nbsp;random&#038;n &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/02\/06\/leetcodeday138\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcodeday138&#8211;\u590d\u5236\u5e26\u968f\u673a\u6307\u9488\u7684\u94fe\u8868<\/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\/2625"}],"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=2625"}],"version-history":[{"count":4,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2625\/revisions"}],"predecessor-version":[{"id":2630,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2625\/revisions\/2630"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2625"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}