Week Training: 406 Queue Reconstruction by Height
来源:互联网 发布:网络摄像机排行榜 编辑:程序博客网 时间:2024/05/22 06:57
Before, I traverse the vector times and times to find the one to insert into the new vector each time, with a low efficiency. However, I was impressed by another ingenious method and used it without hesitation. We first sort the vector descending first element, then the ascending second element. After that, we just insert each element in the sorted vector based on its second value. An amazing way to solve this problem.
class Solution {public: static bool comp(pair<int, int> a, pair<int, int> b){ return a.first > b.first || (a.first == b.first && a.second < b.second); } vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) { vector<pair<int, int>> re; int size = people.size(); sort(people.begin(),people.end(),comp); for(int i=0;i<people.size();i++){ re.insert(re.begin()+people[i].second,people[i]); } return re; }};
0 0
- Week Training: 406 Queue Reconstruction by Height
- 【406】 Queue Reconstruction by Height
- Queue Reconstruction by Height
- Queue Reconstruction by Height
- Queue Reconstruction by Height
- Queue Reconstruction by Height
- Queue Reconstruction by Height
- Queue Reconstruction by Height
- Queue Reconstruction by Height
- Queue Reconstruction by Height
- Queue Reconstruction by Height
- Queue Reconstruction by Height
- Queue Reconstruction by Height
- Queue Reconstruction by Height
- Queue Reconstruction by Height
- LeetCode[406] Queue Reconstruction by Height
- LeetCode #406: Queue Reconstruction by Height
- leetcode 406 Queue Reconstruction by Height C++
- 1.Python基础要点(一)
- 关于srand()函数与rand()函数的问题
- LeetCode-43. Multiply Strings (JAVA)大整数乘法
- 两种写代码的风格(从简而一)
- LeetCode#268. Missing Number
- Week Training: 406 Queue Reconstruction by Height
- Sass在win7环境中的安装
- redis集群——删除节点
- 仿美团登录界面
- Android中带你开发一款自动爆破签名校验工具kstools
- strcpy函数的实现
- VIM快捷(二):插入
- R入门
- def参数--review python