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;
}
};
有帮助的话可以点个赞,我会很开心的~
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?