Skip to content

Commit b5156c8

Browse files
committed
二刷138
1 parent cbe316b commit b5156c8

File tree

6 files changed

+81
-5
lines changed

6 files changed

+81
-5
lines changed

docs/0000-00-note.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ image::images/quick-sort-01.gif[{image_attr}]
2424
* [ ] https://leetcode.cn/problems/number-of-islands/solutions/211211/dao-yu-lei-wen-ti-de-tong-yong-jie-fa-dfs-bian-li-/[200. 岛屿数量 - 岛屿类问题的通用解法、DFS 遍历框架^]
2525
* [ ] 魔改的二分查找
2626
* [ ] 买卖股票
27+
* [ ] 链表复制相关操作,也可以将新旧节点交替相连来处理。
2728

2829
== 解题范式
2930

docs/0138-copy-list-with-random-pointer.adoc

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,43 @@ image::images/0138-copy-list-with-random-pointer-example-3.png[{image_attr}]
5858
* `Node.random` is null or pointing to a node in the linked list.
5959
* Number of Nodes will not exceed 1000.
6060
61-
== 解题分析
61+
== 思路分析
6262

63-
这道题可以使用类似 xref:133. Clone Graph[133. Clone Graph] 的解法来求解。
63+
这道题可以使用类似 xref:0133-clone-graph.adoc[133. Clone Graph] 的解法来求解。
6464

6565
video::OvpKeraoxW0[youtube]
6666

6767
image::images/0138-copy-list-with-random-pointer.jpg[{image_attr}]
6868

69-
这个解法非常精妙!
69+
image::images/0138-02.png[{image_attr}]
70+
71+
新旧节点相连的解法非常精妙!
7072

7173
使用 `Map` 的解法也好巧妙啊!
7274

7375
[[src-0138]]
76+
[tabs]
77+
====
78+
一刷::
79+
+
80+
--
7481
[{java_src_attr}]
7582
----
7683
include::{sourcedir}/_0138_CopyListWithRandomPointer.java[tag=answer]
7784
----
85+
--
86+
87+
二刷::
88+
+
89+
--
90+
[{java_src_attr}]
91+
----
92+
include::{sourcedir}/_0138_CopyListWithRandomPointer_2.java[tag=answer]
93+
----
94+
--
95+
====
96+
97+
== 参考资料
7898

99+
. https://leetcode.cn/problems/copy-list-with-random-pointer/solutions/2361362/138-fu-zhi-dai-sui-ji-zhi-zhen-de-lian-b-6jeo/?envType=study-plan-v2&envId=selected-coding-interview[138. 随机链表的复制 - 清晰图解^]
100+
. https://leetcode.cn/problems/copy-list-with-random-pointer/solutions/889166/fu-zhi-dai-sui-ji-zhi-zhen-de-lian-biao-rblsf/?envType=study-plan-v2&envId=selected-coding-interview[138. 随机链表的复制 - 官方题解^]

docs/images/0138-02.png

56.5 KB
Loading

logbook/202406.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,11 @@
732732
|{doc_base_url}/1823-find-the-winner-of-the-circular-game.adoc[题解]
733733
|❌ 约瑟夫环,使用队列出队入队来模拟计数,出队表示删除。妙!
734734

735+
|{counter:codes}
736+
|{leetcode_base_url}/copy-list-with-random-pointer/[138. Copy List with Random Pointer^]
737+
|{doc_base_url}/0138-copy-list-with-random-pointer.adoc[题解]
738+
|✅ 哈希或者链表新旧交替相连
739+
735740
|===
736741

737742
截止目前,本轮练习一共完成 {codes} 道题。

src/main/java/com/diguage/algo/leetcode/_0138_CopyListWithRandomPointer.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,17 @@
5757
* @since 2020-01-07 22:21
5858
*/
5959
public class _0138_CopyListWithRandomPointer {
60-
// tag::answer[]
60+
// tag::answer[]
6161

6262
/**
6363
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Copy List with Random Pointer.
6464
*
6565
* Memory Usage: 40.6 MB, less than 5.61% of Java online submissions for Copy List with Random Pointer.
6666
*
6767
* Copy from: https://leetcode.com/problems/copy-list-with-random-pointer/discuss/43488/Java-O(n)-solution[Java O(n) solution - LeetCode Discuss]
68+
*
69+
* @author D瓜哥 · https://www.diguage.com
70+
* @since 2020-01-07 22:21
6871
*/
6972
public Node copyRandomList(Node head) {
7073
if (Objects.isNull(head)) {
@@ -182,7 +185,7 @@ public static Node build(Integer[][] array) {
182185
return restult;
183186
}
184187

185-
static class Node {
188+
public static class Node {
186189
int val;
187190
Node next;
188191
Node random;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import static com.diguage.algo.leetcode._0138_CopyListWithRandomPointer.Node;
7+
8+
public class _0138_CopyListWithRandomPointer_2 {
9+
// tag::answer[]
10+
11+
/**
12+
* @author D瓜哥 · https://www.diguage.com
13+
* @since 2024-09-19 15:56:51
14+
*/
15+
public Node copyRandomList(Node head) {
16+
if (head == null) {
17+
return null;
18+
}
19+
Map<Node, Node> nodeToRandomMap = new HashMap<>();
20+
Map<Node, Node> oldToNewMap = new HashMap<>();
21+
Node dummy = new Node(0);
22+
Node curr = head;
23+
Node tail = dummy;
24+
while (curr != null) {
25+
Node newNode = new Node(curr.val);
26+
oldToNewMap.put(curr, newNode);
27+
if (curr.random != null) {
28+
nodeToRandomMap.put(curr, curr.random);
29+
}
30+
tail.next = newNode;
31+
tail = newNode;
32+
curr = curr.next;
33+
}
34+
curr = head;
35+
while (curr != null) {
36+
if (curr.random != null) {
37+
Node newNode = oldToNewMap.get(curr);
38+
newNode.random = oldToNewMap.get(nodeToRandomMap.get(curr));
39+
}
40+
curr = curr.next;
41+
}
42+
return dummy.next;
43+
}
44+
// end::answer[]
45+
}

0 commit comments

Comments
 (0)