當(dāng)前位置:
首頁(yè) >
30/100. Queue Reconstruction by Height
發(fā)布時(shí)間:2024/1/17
45
豆豆
生活随笔
收集整理的這篇文章主要介紹了
30/100. Queue Reconstruction by Height
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
假設(shè)有一個(gè)隨機(jī)的排隊(duì)的人的列表。每個(gè)人都由一對(duì)整數(shù) (h, k) 來描述,其中h是人的身高,k是這個(gè)人前面身高大于或等于h的人數(shù)。要求編寫一個(gè)算法來重建隊(duì)列。
首先將身高從高到底排列,對(duì)排好序的sortOfpeople列表進(jìn)行遍歷,使用inset()函數(shù)插入res列表的k位置。
class Solution(object):def reconstructQueue(self, people):""":type people: List[List[int]]:rtype: List[List[int]]"""sortOfpeople = sorted(people, key = lambda x:(-x[0],x[1]))res = []for x in sortOfpeople:res.insert(x[1],x)return res- 相關(guān)知識(shí)點(diǎn):
insert() 函數(shù):用于將指定對(duì)象插入列表的指定位置。
總結(jié)
以上是生活随笔為你收集整理的30/100. Queue Reconstruction by Height的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ECharts - 气泡图
- 下一篇: 8/100. Find All Numb