File tree Expand file tree Collapse file tree 3 files changed +30
-11
lines changed Expand file tree Collapse file tree 3 files changed +30
-11
lines changed Original file line number Diff line number Diff line change 5
5
require (
6
6
github.com/spf13/cast v1.6.0
7
7
github.com/stretchr/testify v1.9.0
8
+ golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842
8
9
)
9
10
10
11
require (
Original file line number Diff line number Diff line change @@ -2,8 +2,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
2
2
github.com/davecgh/go-spew v1.1.1 /go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38 =
3
3
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8 =
4
4
github.com/frankban/quicktest v1.14.6 /go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0 =
5
- github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38 =
6
- github.com/google/go-cmp v0.5.9 /go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY =
5
+ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI =
6
+ github.com/google/go-cmp v0.6.0 /go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY =
7
7
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE =
8
8
github.com/kr/pretty v0.3.1 /go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk =
9
9
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY =
@@ -16,6 +16,8 @@ github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
16
16
github.com/spf13/cast v1.6.0 /go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo =
17
17
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg =
18
18
github.com/stretchr/testify v1.9.0 /go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY =
19
+ golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 h1:vr/HnozRka3pE4EsMEg1lgkXJkTFJCVUX+S/ZT6wYzM =
20
+ golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 /go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc =
19
21
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM =
20
22
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 /go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0 =
21
23
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA =
Original file line number Diff line number Diff line change 1
1
package leet_03
2
2
3
- type Node interface {
4
- GetId () int
5
- GetPid () int
6
- AppendChildren (Node )
3
+ import (
4
+ "golang.org/x/exp/constraints"
5
+ )
6
+
7
+ type NodeSlice [T constraints.Ordered , E any ] []Node [T , E ]
8
+
9
+ func (nodes NodeSlice [T , E ]) Len () int {
10
+ return len (nodes )
11
+ }
12
+ func (nodes NodeSlice [T , E ]) Less (i , j int ) bool {
13
+ return nodes [i ].GetId () < nodes [j ].GetId ()
14
+ }
15
+ func (nodes NodeSlice [T , E ]) Swap (i , j int ) {
16
+ nodes [i ], nodes [j ] = nodes [j ], nodes [i ]
17
+ }
18
+
19
+ type Node [T constraints.Ordered , E any ] interface {
20
+ GetId () T
21
+ GetPid () T
22
+ AppendChildren (E )
7
23
}
8
24
9
- func IntoTree [T Node ] (rows []T , rootPid int ) []T {
10
- var root []T
11
- nodes := make (map [int ] T , len (rows ))
25
+ func IntoTree [T constraints. Ordered , E Node [ T , E ]] (rows []E , rootPid T ) []E {
26
+ var root []E
27
+ nodes := make (map [T ] E , len (rows ))
12
28
for _ , v := range rows {
13
29
node := v
14
30
id := node .GetId ()
@@ -38,6 +54,6 @@ func (d *Dept) GetPid() int {
38
54
return d .Pid
39
55
}
40
56
41
- func (d * Dept ) AppendChildren (v Node ) {
42
- d .Children = append (d .Children , v .( * Dept ) )
57
+ func (d * Dept ) AppendChildren (v * Dept ) {
58
+ d .Children = append (d .Children , v )
43
59
}
You can’t perform that action at this time.
0 commit comments