自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

聊一聊復(fù)制鏈表的復(fù)制

開發(fā) 前端
請(qǐng)實(shí)現(xiàn) copyRandomList 函數(shù),復(fù)制一個(gè)復(fù)雜鏈表。在復(fù)雜鏈表中,每個(gè)節(jié)點(diǎn)除了有一個(gè) next 指針指向下一個(gè)節(jié)點(diǎn),還有一個(gè) random 指針指向鏈表中的任意節(jié)點(diǎn)或者 null。

[[438684]]

Leetcode : https://leetcode-cn.com/problems/fu-za-lian-biao-de-fu-zhi-lcof

“GitHub : https://gitee.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_28_copyRandomList/Solution.java

復(fù)制鏈表的復(fù)制

“題目描述 :請(qǐng)實(shí)現(xiàn) copyRandomList 函數(shù),復(fù)制一個(gè)復(fù)雜鏈表。在復(fù)雜鏈表中,每個(gè)節(jié)點(diǎn)除了有一個(gè) next 指針指向下一個(gè)節(jié)點(diǎn),還有一個(gè) random 指針指向鏈表中的任意節(jié)點(diǎn)或者 null。難度:中等

示例 1:

  1. 輸入:head = [[7,null],[13,0],[11,4],[10,2],[1,0]] 
  2.  
  3. 輸出:[[7,null],[13,0],[11,4],[10,2],[1,0]] 

示例 2:

  1. 輸入:head = [[1,1],[2,1]] 
  2.  
  3. 輸出:[[1,1],[2,1]] 

示例 3:

  1. 輸入:head = [[3,null],[3,0],[3,null]] 
  2.  
  3. 輸出:[[3,null],[3,0],[3,null]] 

示例 4:

  1. 輸入:head = [] 
  2. 輸出:[] 
  3. 解釋:給定的鏈表為空(空指針),因此返回 null。 

提示:

  • 10000 <= Node.val <= 10000
  • Node.random 為空(null)或指向鏈表中的節(jié)點(diǎn)。
  • 節(jié)點(diǎn)數(shù)目不超過 1000 。

方法:哈希表

“利用哈希表的查詢特點(diǎn),考慮構(gòu)建原鏈表節(jié)點(diǎn)和新鏈表對(duì)應(yīng)節(jié)點(diǎn)的鍵值對(duì)映射關(guān)系,再遍歷構(gòu)建新鏈表各節(jié)點(diǎn)的next 和random 引用指向即可。

算法流程:

  • 若頭節(jié)點(diǎn)head為空節(jié)點(diǎn),直接返回null ;
  • 初始化:哈希表dic ,節(jié)點(diǎn)cur 指向頭節(jié)點(diǎn);
  • 復(fù)制鏈表:
    • 建立新節(jié)點(diǎn),并向dic添加鍵值對(duì)(原cur節(jié)點(diǎn),新cur節(jié)點(diǎn)) ;
    • cur 遍歷至原鏈表下一節(jié)點(diǎn);
  • 構(gòu)建新鏈表的引用指向:
    • 構(gòu)建新節(jié)點(diǎn)的next 和random 弓|用指向;
    • cur 遍歷至原鏈表下一節(jié)點(diǎn);
  • 返回值:新鏈表的頭節(jié)點(diǎn)dic[cur] ;

復(fù)雜度分析:

  • 時(shí)間復(fù)雜度O(N) :兩輪遍歷鏈表,使用O(N)時(shí)間。
  • 空間復(fù)雜度0(N) :哈希表dic 使用線性大小的額外空間。

  1. package com.nateshao.sword_offer.topic_28_copyRandomList; 
  2.  
  3. import java.util.HashMap; 
  4. import java.util.Map; 
  5.  
  6. /** 
  7.  * @date Created by 邵桐杰 on 2021/12/2 14:05 
  8.  * @微信公眾號(hào) 程序員千羽 
  9.  * @個(gè)人網(wǎng)站 www.nateshao.cn 
  10.  * @博客 https://nateshao.gitee.io 
  11.  * @GitHub https://github.com/nateshao 
  12.  * @Gitee https://gitee.com/nateshao 
  13.  * Description:  復(fù)雜鏈表的復(fù)制 
  14.  */ 
  15. public class Solution { 
  16.     /** 
  17.      * 精選解答 
  18.      * @param head 
  19.      * @return 
  20.      */ 
  21.     public Node copyRandomList(Node head) { 
  22.         if(head == nullreturn null
  23.         Node cur = head; 
  24.         Map<Node, Node> map = new HashMap<>(); 
  25.         // 3. 復(fù)制各節(jié)點(diǎn),并建立 “原節(jié)點(diǎn) -> 新節(jié)點(diǎn)” 的 Map 映射 
  26.         while(cur != null) { 
  27.             map.put(cur, new Node(cur.val)); 
  28.             cur = cur.next
  29.         } 
  30.         cur = head; 
  31.         // 4. 構(gòu)建新鏈表的 next 和 random 指向 
  32.         while(cur != null) { 
  33.             map.get(cur).next = map.get(cur.next); 
  34.             map.get(cur).random = map.get(cur.random); 
  35.             cur = cur.next
  36.         } 
  37.         // 5. 返回新鏈表的頭節(jié)點(diǎn) 
  38.         return map.get(head); 
  39.     } 
  40.  
  41.  
  42.     /** 
  43.      * 思路:先復(fù)制鏈表的 next 節(jié)點(diǎn),將復(fù)制后的節(jié)點(diǎn)接在原節(jié)點(diǎn)后,然后復(fù)制其它的 
  44.      * 節(jié)點(diǎn),最后取偶數(shù)位置的節(jié)點(diǎn)(復(fù)制后的節(jié)點(diǎn))。 
  45.      * 
  46.      * @param head 
  47.      * @return 
  48.      */ 
  49.     public Node copyRandomList2(Node head) { 
  50.         if (head == nullreturn null
  51.         Node node = new Node(head.val); 
  52.         Node temp = node; 
  53.  
  54.         while (head.next != null) { 
  55.             temp.next = new Node(head.next.val); 
  56.             if (head.random != null) { 
  57.                 temp.random = new Node(head.random.val); 
  58.             } 
  59.             head = head.next
  60.             temp = temp.next
  61.         } 
  62.         return head; 
  63.     } 
  64.  
  65.     // Definition for a Node. 
  66.     class Node { 
  67.         int val; 
  68.         Node next
  69.         Node random; 
  70.  
  71.         public Node(int val) { 
  72.             this.val = val; 
  73.             this.next = null
  74.             this.random = null
  75.         } 
  76.     } 

參考鏈接:https://leetcode-cn.com/problems/fu-za-lian-biao-de-fu-zhi-lcof/solution/jian-zhi-offer-35-fu-za-lian-biao-de-fu-zhi-ha-xi-

 

責(zé)任編輯:武曉燕 來源: 程序員千羽
相關(guān)推薦

2022-06-27 07:50:16

鏈表節(jié)點(diǎn)測(cè)試

2023-07-06 13:56:14

微軟Skype

2020-09-08 06:54:29

Java Gradle語(yǔ)言

2023-09-22 17:36:37

2021-01-28 22:31:33

分組密碼算法

2020-05-22 08:16:07

PONGPONXG-PON

2018-06-07 13:17:12

契約測(cè)試單元測(cè)試API測(cè)試

2021-08-01 09:55:57

Netty時(shí)間輪中間件

2023-09-27 16:39:38

2024-10-28 21:02:36

消息框應(yīng)用程序

2021-03-01 18:37:15

MySQL存儲(chǔ)數(shù)據(jù)

2023-09-20 23:01:03

Twitter算法

2021-07-16 11:48:26

模型 .NET微軟

2021-08-04 09:32:05

Typescript 技巧Partial

2022-08-08 08:25:21

Javajar 文件

2022-11-01 08:46:20

責(zé)任鏈模式對(duì)象

2018-11-29 09:13:47

CPU中斷控制器

2019-02-13 14:15:59

Linux版本Fedora

2021-01-29 08:32:21

數(shù)據(jù)結(jié)構(gòu)數(shù)組

2021-02-06 08:34:49

函數(shù)memoize文檔
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)