Skip to content

Commit 3d61b13

Browse files
committed
添加406. 根据身高重建队列 go版本 链表法
1 parent 9fa333e commit 3d61b13

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

problems/0406.根据身高重建队列.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ class Solution:
222222
```
223223

224224
Go:
225-
```golang
225+
```go
226226
func 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+
244274
Javascript:
275+
245276
```Javascript
246277
var reconstructQueue = function(people) {
247278
let queue = []

0 commit comments

Comments
 (0)