mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
添加406. 根据身高重建队列 go版本 链表法
This commit is contained in:
@ -222,7 +222,7 @@ class Solution:
|
|||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
```golang
|
```go
|
||||||
func reconstructQueue(people [][]int) [][]int {
|
func reconstructQueue(people [][]int) [][]int {
|
||||||
//先将身高从大到小排序,确定最大个子的相对位置
|
//先将身高从大到小排序,确定最大个子的相对位置
|
||||||
sort.Slice(people,func(i,j int)bool{
|
sort.Slice(people,func(i,j int)bool{
|
||||||
@ -241,7 +241,38 @@ func reconstructQueue(people [][]int) [][]int {
|
|||||||
return result
|
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:
|
||||||
|
|
||||||
```Javascript
|
```Javascript
|
||||||
var reconstructQueue = function(people) {
|
var reconstructQueue = function(people) {
|
||||||
let queue = []
|
let queue = []
|
||||||
|
Reference in New Issue
Block a user