30/100. Queue Reconstruction by Height
生活随笔
收集整理的這篇文章主要介紹了
30/100. Queue Reconstruction by Height
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
假設(shè)有一個隨機的排隊的人的列表。每個人都由一對整數(shù) (h, k) 來描述,其中h是人的身高,k是這個人前面身高大于或等于h的人數(shù)。要求編寫一個算法來重建隊列。
首先將身高從高到底排列,對排好序的sortOfpeople列表進行遍歷,使用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)知識點:
insert() 函數(shù):用于將指定對象插入列表的指定位置。
總結(jié)
以上是生活随笔為你收集整理的30/100. Queue Reconstruction by Height的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ECharts - 气泡图
- 下一篇: Navicat 10 for SQL S