Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7abdc6e

Browse files
add 1490
1 parent03e2748 commit7abdc6e

File tree

4 files changed

+88
-4
lines changed

4 files changed

+88
-4
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ _If you like this project, please leave me a star._ ★
6969
|1493|[Longest Subarray of 1's After Deleting One Element](https://leetcode.com/problems/longest-subarray-of-1s-after-deleting-one-element/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1493.java)|[:tv:](https://youtu.be/nKhteIRZ2Ok)|Medium|Array|
7070
|1492|[The kth Factor of n](https://leetcode.com/problems/the-kth-factor-of-n/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1492.java)||Medium|Math|
7171
|1491|[Average Salary Excluding the Minimum and Maximum Salary](https://leetcode.com/problems/average-salary-excluding-the-minimum-and-maximum-salary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1491.java)||Easy|Array, Sort|
72+
|1490|[Clone N-ary Tree](https://leetcode.com/problems/clone-n-ary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1490.java)||Medium|HashTable, Tree, DFS, BFS|
7273
|1487|[Making File Names Unique](https://leetcode.com/problems/making-file-names-unique/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1487.java)||Medium|HashTable, String|
7374
|1486|[XOR Operation in an Array](https://leetcode.com/problems/xor-operation-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1486.java)||Medium|Array, Bit Manipulation|
7475
|1481|[Least Number of Unique Integers after K Removals](https://leetcode.com/problems/least-number-of-unique-integers-after-k-removals/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1481.java)||Medium|Array, Sort|

‎src/main/java/com/fishercoder/common/classes/Node.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@
22

33
importjava.util.ArrayList;
44
importjava.util.List;
5+
importjava.util.Objects;
56

67
publicclassNode {
78
publicintval;
89
publicList<Node>children;
910

1011
publicNode() {
12+
this.children =newArrayList<>();
1113
}
1214

13-
publicNode(intval,List<Node>children) {
15+
publicNode(intval) {
1416
this.val =val;
15-
this.children =children;
17+
this.children =newArrayList<>();
1618
}
1719

18-
publicNode(intval) {
20+
publicNode(intval,List<Node>children) {
1921
this.val =val;
20-
this.children =newArrayList<>();
22+
this.children =children;
2123
}
2224

2325
//todo: implement this method
@@ -28,4 +30,21 @@ public Node(int val) {
2830
publicstaticNodecreateNaryTree(List<Integer>preorderValues) {
2931
returnnull;
3032
}
33+
34+
@Override
35+
publicbooleanequals(Objecto) {
36+
if (this ==o) {
37+
returntrue;
38+
}
39+
if (o ==null ||getClass() !=o.getClass()) {
40+
returnfalse;
41+
}
42+
Nodenode = (Node)o;
43+
returnval ==node.val &&Objects.equals(children,node.children);
44+
}
45+
46+
@Override
47+
publicinthashCode() {
48+
returnObjects.hash(val,children);
49+
}
3150
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importcom.fishercoder.common.classes.Node;
4+
5+
importjava.util.HashMap;
6+
importjava.util.LinkedList;
7+
importjava.util.Map;
8+
importjava.util.Queue;
9+
10+
publicclass_1490 {
11+
publicstaticclassSolution1 {
12+
publicNodecloneTree(Noderoot) {
13+
if (root ==null) {
14+
returnroot;
15+
}
16+
Map<Node,Node>map =newHashMap<>();
17+
map.put(root,newNode(root.val));
18+
Queue<Node>queue =newLinkedList<>();
19+
queue.offer(root);
20+
while (!queue.isEmpty()) {
21+
Nodecurr =queue.poll();
22+
for (Nodechild :curr.children) {
23+
NodechildCopy =newNode(child.val);
24+
if (!map.containsKey(child)) {
25+
map.put(child,childCopy);
26+
queue.offer(child);
27+
}
28+
map.get(curr).children.add(childCopy);
29+
}
30+
}
31+
returnmap.get(root);
32+
}
33+
}
34+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.classes.Node;
4+
importcom.fishercoder.solutions._1490;
5+
importorg.junit.BeforeClass;
6+
importorg.junit.Test;
7+
8+
importstaticorg.junit.Assert.assertEquals;
9+
10+
publicclass_1490Test {
11+
privatestatic_1490.Solution1solution1;
12+
13+
@BeforeClass
14+
publicstaticvoidsetup() {
15+
solution1 =new_1490.Solution1();
16+
}
17+
18+
@Test
19+
publicvoidtest1() {
20+
Noderoot =newNode(8);
21+
Nodechild =newNode(1);
22+
root.children.add(child);
23+
NodeleftGrandChild =newNode(8);
24+
NoderightGrandChild =newNode(5);
25+
child.children.add(leftGrandChild);
26+
child.children.add(rightGrandChild);
27+
assertEquals(root,solution1.cloneTree(root));
28+
}
29+
30+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp