首页 > 其他 > 详细

LeetCode 406. Queue Reconstruction by Height

时间:2018-11-10 10:25:36      阅读:99      评论:0      收藏:0      [点我收藏+]

先根据身高排序,高的在前。然后对people进行遍历(从高到矮放入队伍),对于当前的人来说,比他高的人都已经排好队了,因此只要根据k插入队伍即可。

代码非常简洁。

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){
            if (a.first==b.first) return a.second<b.second;
            return a.first>b.first;
        });
        vector<pair<int,int>> res;
        for (auto x:people)
            res.insert(res.begin()+x.second, x);
        return res;
    }
};

 

LeetCode 406. Queue Reconstruction by Height

原文:https://www.cnblogs.com/hankunyan/p/9938252.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!