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

Commit ebb28c3

Browse files
authored
Improved Node.toString()
1 parent 206b5b8 commit ebb28c3

File tree

6 files changed

+25
-87
lines changed

6 files changed

+25
-87
lines changed

‎src/main/kotlin/com_github_leetcode/neighbors/Node.kt

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com_github_leetcode.random
22

3-
import java.util.StringJoiner
4-
53
class Node {
64
var `val`: Int
75
var next: Node? = null
@@ -12,33 +10,38 @@ class Node {
1210
}
1311

1412
override fun toString(): String {
15-
val result = StringJoiner(",", "[", "]")
16-
val result2 = StringJoiner(",", "[", "]")
17-
result2.add(`val`.toString())
13+
val result = StringBuilder()
14+
result.append("[")
15+
result.append("[")
16+
result.append(`val`)
17+
result.append(",")
1818
if (random == null) {
19-
result2.add("null")
19+
result.append("null")
2020
} else {
21-
result2.add(random!!.`val`.toString())
21+
result.append(random!!.`val`)
2222
}
23-
result.add(result2.toString())
23+
result.append("]")
2424
var curr = next
2525
while (curr != null) {
26-
val result3 = StringJoiner(",", "[", "]")
27-
result3.add(curr.`val`.toString())
26+
result.append(",")
27+
result.append("[")
28+
result.append(curr.`val`)
29+
result.append(",")
2830
if (curr.random == null) {
29-
result3.add("null")
31+
result.append("null")
3032
} else {
3133
var randomIndex = 0
32-
var curr2: Node? = this
33-
while (curr2!!.next != null && curr2 !== curr.random) {
34-
randomIndex+=1
35-
curr2 = curr2.next
34+
var indexFinder: Node? = this
35+
while (indexFinder!!.next != null && indexFinder !== curr.random) {
36+
randomIndex++
37+
indexFinder = indexFinder.next
3638
}
37-
result3.add(randomIndex.toString())
39+
result.append(randomIndex)
3840
}
39-
result.add(result3.toString())
41+
result.append("]")
4042
curr = curr.next
4143
}
44+
result.append("]")
4245
return result.toString()
4346
}
4447
}

‎src/main/kotlin/g0101_0200/s0133_clone_graph/Solution.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package g0101_0200.s0133_clone_graph
22

33
// #Medium #Hash_Table #Depth_First_Search #Breadth_First_Search #Graph #Udemy_Graph
4-
// #Top_Interview_150_Graph_General #2022_10_09_Time_351_ms_(60.91%)_Space_37.1_MB_(70.56%)
4+
// #Top_Interview_150_Graph_General #2025_08_03_Time_133_ms_(88.96%)_Space_43.11_MB_(67.94%)
55

6-
import com_github_leetcode.neighbors.Node
6+
import com_github_leetcode.Node
77

88
/*
99
* Definition for a Node.
@@ -24,11 +24,10 @@ class Solution {
2424
}
2525
val newNode = Node(node.`val`)
2626
processedNodes[node] = newNode
27-
newNode.neighbors = ArrayList()
2827
for (neighbor in node.neighbors) {
29-
val clonedNeighbor:Node? = cloneGraph(neighbor, processedNodes)
28+
val clonedNeighbor = cloneGraph(neighbor, processedNodes)
3029
if (clonedNeighbor != null) {
31-
newNode.neighbors.add(clonedNeighbor)
30+
(newNode.neighborsasArrayList).add(clonedNeighbor)
3231
}
3332
}
3433
return newNode

‎src/test/kotlin/com_github_leetcode/NodeTest.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@ import org.hamcrest.MatcherAssert.assertThat
55
import org.junit.jupiter.api.Test
66

77
internal class NodeTest {
8-
@Test
9-
fun constructor() {
10-
val node = Node()
11-
assertThat(node.`val`, equalTo(0))
12-
assertThat(node.toString(), equalTo("[]"))
13-
}
14-
158
@Test
169
fun constructor2() {
1710
val node = Node(1)

‎src/test/kotlin/com_github_leetcode/neighbors/NodeTest.kt

Lines changed: 0 additions & 30 deletions
This file was deleted.

‎src/test/kotlin/g0101_0200/s0133_clone_graph/SolutionTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package g0101_0200.s0133_clone_graph
22

3-
import com_github_leetcode.neighbors.Node
3+
import com_github_leetcode.Node
44
import org.hamcrest.CoreMatchers.equalTo
55
import org.hamcrest.MatcherAssert.assertThat
66
import org.junit.jupiter.api.Test

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /