Leetcode 406 - Queue Reconstruction by Height

143 字
1 分钟
Leetcode 406 - Queue Reconstruction by Height

耗时:1h

瞟了一眼题解看到要先对people中的每个元素做排序,然后尝试了按身高排和按前面有几个人的身高>=自己排, 发现按身高从高到低排能解决前两个用例:[[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]和[[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]],

Arrays.sort(people, Comparator.comparingInt((int[] a) -> a[0]).reversed());

于是愉快的进行提交:

针对出错的用例逐行debug,发现还得加一个排序规则:按前面有几个人的身高>=自己 从低到高

Arrays.sort(people, (int[] a, int[] b) -> a[0] == b[0] ? a[1] - b[1] : b[0] - a[0]);

再次进行提交:

🎉🎉

code

支持与分享

如果这篇文章对你有帮助,欢迎分享给更多人或赞助支持!

赞助
Leetcode 406 - Queue Reconstruction by Height
https://blog.xoxxo18.icu/posts/leetcode-406/
作者
Kyson
发布于
2026-03-22
许可协议
CC BY-NC-SA 4.0
随机文章 随机推荐

评论区

Profile Image of the Author
Kyson
君子终日乾乾,夕惕若厉,无咎。
公告
欢迎来到我的博客!
音乐
封面

音乐

暂未播放

0:00 0:00
暂无歌词
分类
标签
站点统计
文章
2
分类
2
标签
2
总字数
1,680
运行时长
0
最后活动
0 天前

目录