导购个人网站怎么做的,怎么做二级网站域名,广州 电商网站建设,昆明旅游网站建设栈与队列 Leetcode 347 前k个高频元素
Leetcode 347
灵活运用C库函数#xff0c;使用匿名函数排序#xff0c;sort可以替换为快排实现#xff08;面试感觉可能会手撕#xff0c;机考直接使用sort#xff09;
class Solution {
public:vectorint topKFrequent(v…栈与队列 Leetcode 347 前k个高频元素
Leetcode 347
灵活运用C库函数使用匿名函数排序sort可以替换为快排实现面试感觉可能会手撕机考直接使用sort
class Solution {
public:vectorint topKFrequent(vectorint nums, int k) {mapint, int num_f;for(int i 0; i nums.size(); i){num_f[nums[i]];}vectorpairint, int num_f_vec;mapint, int::iterator it;for(it num_f.begin(); it ! num_f.end(); it){// push_back()是创建对象后在复制到vector末尾而emplace_back是就地构建对象没有复制和移动的操作提高复杂对象到vector的性能num_f_vec.emplace_back(it-first, it-second);}sort(num_f_vec.begin(), num_f_vec.end(), [](pairint, int a, pairint, int b){return a.second b.second;});vectorint res;for(int i 0; i k; i){res.push_back(num_f_vec[i].first);}return res;}
};