记载下这个题中的语法(对这些语法的使用不熟悉)
生活随笔
收集整理的这篇文章主要介绍了
记载下这个题中的语法(对这些语法的使用不熟悉)
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
这个题的解法按照身高h降序和k升序对数组中的元素进行排列,然后插入的时候按照索引进行插入
class Solution { public:vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {//身高降序排列,人数升序排列 //自定义排序方式,因为是vector<vector<int>>类型的,所以需要自己定义sort的比较函数。a[0]代表第一//people[i]的第一个元素。sort(people.begin(), people.end(), [](const vector<int>& a, const vector<int>& b){if(a[0] > b[0]) return true;if(a[0] == b[0] && a[1] <b[1])return true;return false;});vector<vector<int>> res;for(int i = 0; i < people.size(); i++){res.insert(res.begin() + people[i][1], people[i]);//resert函数在res.begin() + //people[i][1],位置之前插入people[i].}/*for(auto& e : people){res.insert(res.begin() + e[1], e);}*/return res;} };
总结
以上是生活随笔为你收集整理的记载下这个题中的语法(对这些语法的使用不熟悉)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 一道题教会你回溯、动态规划、贪心
- 下一篇: 车机app