File tree Expand file tree Collapse file tree 6 files changed +81
-5
lines changed
src/main/java/com/diguage/algo/leetcode Expand file tree Collapse file tree 6 files changed +81
-5
lines changed Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ image::images/quick-sort-01.gif[{image_attr}]
24
24
* [ ] https://leetcode.cn/problems/number-of-islands/solutions/211211/dao-yu-lei-wen-ti-de-tong-yong-jie-fa-dfs-bian-li-/[200. 岛屿数量 - 岛屿类问题的通用解法、DFS 遍历框架^]
25
25
* [ ] 魔改的二分查找
26
26
* [ ] 买卖股票
27
+ * [ ] 链表复制相关操作,也可以将新旧节点交替相连来处理。
27
28
28
29
== 解题范式
29
30
Original file line number Diff line number Diff line change @@ -58,21 +58,43 @@ image::images/0138-copy-list-with-random-pointer-example-3.png[{image_attr}]
58
58
* `Node.random` is null or pointing to a node in the linked list.
59
59
* Number of Nodes will not exceed 1000.
60
60
61
- == 解题分析
61
+ == 思路分析
62
62
63
- 这道题可以使用类似 xref:133. Clone Graph [133. Clone Graph] 的解法来求解。
63
+ 这道题可以使用类似 xref:0133-clone-graph.adoc [133. Clone Graph] 的解法来求解。
64
64
65
65
video::OvpKeraoxW0[youtube]
66
66
67
67
image::images/0138-copy-list-with-random-pointer.jpg[{image_attr}]
68
68
69
- 这个解法非常精妙!
69
+ image::images/0138-02.png[{image_attr}]
70
+
71
+ 新旧节点相连的解法非常精妙!
70
72
71
73
使用 `Map` 的解法也好巧妙啊!
72
74
73
75
[[src-0138]]
76
+ [tabs]
77
+ ====
78
+ 一刷::
79
+ +
80
+ --
74
81
[{java_src_attr}]
75
82
----
76
83
include::{sourcedir}/_0138_CopyListWithRandomPointer.java[tag=answer]
77
84
----
85
+ --
86
+
87
+ 二刷::
88
+ +
89
+ --
90
+ [{java_src_attr}]
91
+ ----
92
+ include::{sourcedir}/_0138_CopyListWithRandomPointer_2.java[tag=answer]
93
+ ----
94
+ --
95
+ ====
96
+
97
+ == 参考资料
78
98
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. 随机链表的复制 - 官方题解^]
Original file line number Diff line number Diff line change 732
732
|{doc_base_url} /1823-find-the-winner-of-the-circular-game.adoc[题解]
733
733
|❌ 约瑟夫环,使用队列出队入队来模拟计数,出队表示删除。妙!
734
734
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
+
735
740
|===
736
741
737
742
截止目前,本轮练习一共完成 {codes} 道题。
Original file line number Diff line number Diff line change 57
57
* @since 2020-01-07 22:21
58
58
*/
59
59
public class _0138_CopyListWithRandomPointer {
60
- // tag::answer[]
60
+ // tag::answer[]
61
61
62
62
/**
63
63
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Copy List with Random Pointer.
64
64
*
65
65
* Memory Usage: 40.6 MB, less than 5.61% of Java online submissions for Copy List with Random Pointer.
66
66
*
67
67
* 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
68
71
*/
69
72
public Node copyRandomList (Node head ) {
70
73
if (Objects .isNull (head )) {
@@ -182,7 +185,7 @@ public static Node build(Integer[][] array) {
182
185
return restult ;
183
186
}
184
187
185
- static class Node {
188
+ public static class Node {
186
189
int val ;
187
190
Node next ;
188
191
Node random ;
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments