From e7f4e9f1e9e4e859bf187306fb9ba7c9b3bf5cc6 Mon Sep 17 00:00:00 2001 From: huangge1199 Date: Tue, 9 Feb 2021 15:54:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=9B=E6=89=A3=EF=BC=9A138.=20=E5=A4=8D?= =?UTF-8?q?=E5=88=B6=E5=B8=A6=E9=9A=8F=E6=9C=BA=E6=8C=87=E9=92=88=E7=9A=84?= =?UTF-8?q?=E9=93=BE=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/code/leet/entiy/ListNode.java | 30 +++++++- .../main/java/com/code/leet/entiy/Node.java | 13 ++++ .../java/com/code/leet/entiy/TreeNode.java | 18 ++++- .../leet/study/t20210209/CopyRandomList.java | 73 +++++++++++++++++++ 4 files changed, 129 insertions(+), 5 deletions(-) create mode 100644 LeetCode/src/main/java/com/code/leet/entiy/Node.java create mode 100644 LeetCode/src/main/java/com/code/leet/study/t20210209/CopyRandomList.java diff --git a/LeetCode/src/main/java/com/code/leet/entiy/ListNode.java b/LeetCode/src/main/java/com/code/leet/entiy/ListNode.java index c6d7b12..b91b412 100644 --- a/LeetCode/src/main/java/com/code/leet/entiy/ListNode.java +++ b/LeetCode/src/main/java/com/code/leet/entiy/ListNode.java @@ -1,5 +1,7 @@ package com.code.leet.entiy; +import java.util.List; + /** * @Author: hyy * @Date: 2020-02-12 14:36 @@ -9,7 +11,31 @@ public class ListNode { public int val; public ListNode next; - public ListNode(int x) { - val = x; + public ListNode() {} + public ListNode(int val) { + this.val = val; + } + public ListNode(int val, ListNode next) { this.val = val; this.next = next; } + + public ListNode(List list){ + if(list.size()==0){ + return; + } + if(list.size()==1){ + this.val = list.get(0); + return; + } + if(list.size()==2){ + this.val = list.get(0); + this.next = new ListNode(list.get(1)); + return; + } + this.val = list.get(0); + this.next = new ListNode(list.get(1)); + ListNode newHead = this.next; + for (int i = 2; i < list.size(); i++) { + newHead.next = new ListNode(list.get(i)); + newHead = newHead.next; + } } } diff --git a/LeetCode/src/main/java/com/code/leet/entiy/Node.java b/LeetCode/src/main/java/com/code/leet/entiy/Node.java new file mode 100644 index 0000000..c811715 --- /dev/null +++ b/LeetCode/src/main/java/com/code/leet/entiy/Node.java @@ -0,0 +1,13 @@ +package com.code.leet.entiy; + +public class Node { + public int val; + public Node next; + public Node random; + + public Node(int val) { + this.val = val; + this.next = null; + this.random = null; + } +} diff --git a/LeetCode/src/main/java/com/code/leet/entiy/TreeNode.java b/LeetCode/src/main/java/com/code/leet/entiy/TreeNode.java index 06be834..f06fb34 100644 --- a/LeetCode/src/main/java/com/code/leet/entiy/TreeNode.java +++ b/LeetCode/src/main/java/com/code/leet/entiy/TreeNode.java @@ -1,13 +1,25 @@ package com.code.leet.entiy; + /** * @Author: hyy * @Date: 2020-02-13 18:25 */ - public class TreeNode { - public int val; public TreeNode left; + public Integer val; public TreeNode right; - public TreeNode(int x) { val = x; } + + TreeNode() { + } + + public TreeNode(int val) { + this.val = val; + } + + TreeNode(int val, TreeNode left, TreeNode right) { + this.val = val; + this.left = left; + this.right = right; + } } diff --git a/LeetCode/src/main/java/com/code/leet/study/t20210209/CopyRandomList.java b/LeetCode/src/main/java/com/code/leet/study/t20210209/CopyRandomList.java new file mode 100644 index 0000000..a5f45dc --- /dev/null +++ b/LeetCode/src/main/java/com/code/leet/study/t20210209/CopyRandomList.java @@ -0,0 +1,73 @@ +package com.code.leet.study.t20210209; + +import com.code.leet.entiy.Node; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * 给你一个长度为 n 的链表,每个节点包含一个额外增加的随机指针 random ,该指针可以指向链表中的任何节点或空节点。 + *

+ * 构造这个链表的 深拷贝。 深拷贝应该正好由 n 个 全新 节点组成,其中每个新节点的值都设为其对应的原节点的值。新节点的 next 指针和 random 指针也都应指向复制链表中的新节点,并使原链表和复制链表中的这些指针能够表示相同的链表状态。复制链表中的指针都不应指向原链表中的节点 。 + *

+ * 例如,如果原链表中有 X 和 Y 两个节点,其中 X.random --> Y 。那么在复制链表中对应的两个节点 x 和 y ,同样有 x.random --> y 。 + *

+ * 返回复制链表的头节点。 + *

+ * 用一个由 n 个节点组成的链表来表示输入/输出中的链表。每个节点用一个 [val, random_index] 表示: + *

+ * val:一个表示 Node.val 的整数。 + * random_index:随机指针指向的节点索引(范围从 0 到 n-1);如果不指向任何节点,则为 null 。 + * 你的代码 只 接受原链表的头节点 head 作为传入参数。 + */ +public class CopyRandomList { + + /** + * 138. 复制带随机指针的链表 + */ + public Node copyRandomList(Node head) { + Map map = new HashMap<>(); + return copy(head, map); + } + + private Node copy(Node head, Map map) { + if (head == null) { + return null; + } + if (map.containsKey(head)) { + return map.get(head); + } + Node node = new Node(head.val); + map.put(head, node); + node.next = copy(head.next, map); + node.random = copy(head.random, map); + return node; + } +// public Node copyRandomList(Node head) { +// if (head == null) { +// return null; +// } +// Node newHead = new Node(head.val); +// Node temp = newHead; +// List list = new ArrayList<>(); +// List newlist = new ArrayList<>(); +// list.add(head); +// newlist.add(temp); +// head = head.next; +// while (head != null) { +// list.add(head); +// temp.next = new Node(head.val); +// head = head.next; +// temp = temp.next; +// newlist.add(temp); +// } +// int size = list.size(); +// for (int i = 0; i < size; i++) { +// newlist.get(i).random = list.get(i).random == null ? null : newlist.get(list.indexOf(list.get(i).random)); +// } +// +// return newHead; +// } +}