File tree Expand file tree Collapse file tree 1 file changed +32
-1
lines changed Expand file tree Collapse file tree 1 file changed +32
-1
lines changed Original file line number Diff line number Diff line change @@ -222,7 +222,7 @@ class Solution:
222222```
223223
224224Go:
225- ``` golang
225+ ``` go
226226func reconstructQueue (people [][]int ) [][]int {
227227 // 先将身高从大到小排序,确定最大个子的相对位置
228228 sort.Slice (people,func (i,j int )bool {
@@ -241,7 +241,38 @@ func reconstructQueue(people [][]int) [][]int {
241241 return result
242242}
243243```
244+ ``` go
245+ // 链表法
246+ func reconstructQueue (people [][]int ) [][]int {
247+ sort.Slice (people,func (i,j int ) bool {
248+ if people[i][0 ]==people[j][0 ]{
249+ return people[i][1 ]<people[j][1 ]// 当身高相同时,将K按照从小到大排序
250+ }
251+ // 先将身高从大到小排序,确定最大个子的相对位置
252+ return people[i][0 ]>people[j][0 ]
253+ })
254+ l := list.New ()// 创建链表
255+ for i := 0 ;i<len (people);i++{
256+ position := people[i][1 ]
257+ mark := l.PushBack (people[i])// 插入元素
258+ e := l.Front ()
259+ for position!=0 {// 获取相对位置
260+ position--
261+ e=e.Next ()
262+ }
263+ l.MoveBefore (mark,e)// 移动位置
264+
265+ }
266+ res := [][]int {}
267+ for e := l.Front ();e!=nil ;e=e.Next (){
268+ res=append (res,e.Value .([]int ))
269+ }
270+ return res
271+ }
272+ ```
273+
244274Javascript:
275+
245276``` Javascript
246277var reconstructQueue = function (people ) {
247278 let queue = []
You can’t perform that action at this time.
0 commit comments