Skip to content

Commit

Permalink
fix: add sort
Browse files Browse the repository at this point in the history
  • Loading branch information
jianggb committed May 22, 2024
1 parent e1ceb06 commit 6aabb75
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
1 change: 1 addition & 0 deletions thinkgos/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22
require (
github.com/spf13/cast v1.6.0
github.com/stretchr/testify v1.9.0
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842
)

require (
Expand Down
6 changes: 4 additions & 2 deletions thinkgos/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand All @@ -16,6 +16,8 @@ github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 h1:vr/HnozRka3pE4EsMEg1lgkXJkTFJCVUX+S/ZT6wYzM=
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
34 changes: 25 additions & 9 deletions thinkgos/leet_03/leet_03.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
package leet_03

type Node interface {
GetId() int
GetPid() int
AppendChildren(Node)
import (
"golang.org/x/exp/constraints"
)

type NodeSlice[T constraints.Ordered, E any] []Node[T, E]

func (nodes NodeSlice[T, E]) Len() int {
return len(nodes)
}
func (nodes NodeSlice[T, E]) Less(i, j int) bool {
return nodes[i].GetId() < nodes[j].GetId()
}
func (nodes NodeSlice[T, E]) Swap(i, j int) {
nodes[i], nodes[j] = nodes[j], nodes[i]
}

type Node[T constraints.Ordered, E any] interface {
GetId() T
GetPid() T
AppendChildren(E)
}

func IntoTree[T Node](rows []T, rootPid int) []T {
var root []T
nodes := make(map[int]T, len(rows))
func IntoTree[T constraints.Ordered, E Node[T, E]](rows []E, rootPid T) []E {
var root []E
nodes := make(map[T]E, len(rows))
for _, v := range rows {
node := v
id := node.GetId()
Expand Down Expand Up @@ -38,6 +54,6 @@ func (d *Dept) GetPid() int {
return d.Pid
}

func (d *Dept) AppendChildren(v Node) {
d.Children = append(d.Children, v.(*Dept))
func (d *Dept) AppendChildren(v *Dept) {
d.Children = append(d.Children, v)
}

0 comments on commit 6aabb75

Please sign in to comment.