Skip to content

Commit 6aabb75

Browse files
author
jianggb
committed
fix: add sort
1 parent e1ceb06 commit 6aabb75

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

thinkgos/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.22
55
require (
66
github.com/spf13/cast v1.6.0
77
github.com/stretchr/testify v1.9.0
8+
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842
89
)
910

1011
require (

thinkgos/go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
22
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
33
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
44
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=
77
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
88
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
99
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
@@ -16,6 +16,8 @@ github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
1616
github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
1717
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
1818
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=
1921
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2022
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2123
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

thinkgos/leet_03/leet_03.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
package leet_03
22

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)
723
}
824

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))
1228
for _, v := range rows {
1329
node := v
1430
id := node.GetId()
@@ -38,6 +54,6 @@ func (d *Dept) GetPid() int {
3854
return d.Pid
3955
}
4056

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)
4359
}

0 commit comments

Comments
 (0)