码迷,mamicode.com
首页 > 其他好文 > 详细

406 Queue Reconstruction by Height 根据身高重建队列

时间:2018-04-16 14:42:41      阅读:344      评论:0      收藏:0      [点我收藏+]

标签:com   www   second   bsp   auto   log   turn   logs   一个   

假设有打乱顺序的一群人站成一个队列。 每个人由一个整数对(h, k)表示,其中h是这个人的身高,k是排在这个人前面且身高大于或等于h的人数。 编写一个算法来重建这个队列。
注意:
总人数少于1100人。
示例
输入:
[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
输出:
[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]

详见:https://leetcode.com/problems/queue-reconstruction-by-height/description/

C++:

class Solution {
public:
    vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) {
        sort(people.begin(), people.end(), [](const pair<int,int> &a, const pair<int, int> &b){
            return a.first > b.first || (a.first == b.first && a.second < b.second);
        });
        for (int i = 0; i < people.size(); i++) 
        {
            auto p = people[i];
            if (p.second != i) 
            {
                people.erase(people.begin() + i);
                people.insert(people.begin() + p.second, p);
            }
        }
        return people;
    }
};

 参考:https://www.cnblogs.com/grandyang/p/5928417.html

406 Queue Reconstruction by Height 根据身高重建队列

标签:com   www   second   bsp   auto   log   turn   logs   一个   

原文地址:https://www.cnblogs.com/xidian2014/p/8855449.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!