Skip to content

Commit

Permalink
添加406. 根据身高重建队列 go版本 链表法
Browse files Browse the repository at this point in the history
  • Loading branch information
baici1 committed Sep 23, 2021
1 parent 9fa333e commit 3d61b13
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion problems/0406.根据身高重建队列.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ class Solution:
```

Go:
```golang
```go
func reconstructQueue(people [][]int) [][]int {
//先将身高从大到小排序,确定最大个子的相对位置
sort.Slice(people,func(i,j int)bool{
Expand All @@ -241,7 +241,38 @@ func reconstructQueue(people [][]int) [][]int {
return result
}
```
```go
//链表法
func reconstructQueue(people [][]int) [][]int {
sort.Slice(people,func (i,j int) bool {
if people[i][0]==people[j][0]{
return people[i][1]<people[j][1]//当身高相同时,将K按照从小到大排序
}
//先将身高从大到小排序,确定最大个子的相对位置
return people[i][0]>people[j][0]
})
l:=list.New()//创建链表
for i:=0;i<len(people);i++{
position:=people[i][1]
mark:=l.PushBack(people[i])//插入元素
e:=l.Front()
for position!=0{//获取相对位置
position--
e=e.Next()
}
l.MoveBefore(mark,e)//移动位置

}
res:=[][]int{}
for e:=l.Front();e!=nil;e=e.Next(){
res=append(res,e.Value.([]int))
}
return res
}
```

Javascript:

```Javascript
var reconstructQueue = function(people) {
let queue = []
Expand Down

0 comments on commit 3d61b13

Please sign in to comment.