LeetCode 406. 根据身高重建队列

class Solution {
public:
    struct node
    {
        int val;
        int pre;
        node* next;
        node(int a,int b,node* c)
        {
            val=a;
            pre=b;
            next=c;
        }
    };
    void insert(node* &head,int h,int pre)
    {
        auto p=head;
        int cnt=0;
        while(p->next&&cnt<pre)
        {
            if(p->next->val>=h)   cnt++;
            p=p->next;
        }
        p->next=new node(h,pre,p->next);   
    }
    static bool cmp(vector<int> &a, vector<int> &b) 
    {
        if(a[0]==b[0])  return a[1]<b[1];//要求少的先排
        else return a[0]>b[0];//高的先排
    };
    vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
        auto head=new node(-1,-1,NULL);
    sort(people.begin(), people.end(), cmp);
        vector<vector<int>> res;
        for(auto q:people)
        {
            int h=q[0],pre=q[1];
            insert(head,h,pre);
        }
        for(auto p=head->next;p;p=p->next)
            res.push_back({p->val,p->pre});
        return res;
    }
};
posted @   穿过雾的阴霾  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示