当前位置: 首页 > news >正文

栈与队列 Leetcode 347 前k个高频元素

栈与队列 Leetcode 347 前k个高频元素

Leetcode 347

灵活运用C++库函数,使用匿名函数排序,sort可以替换为快排实现(面试感觉可能会手撕,机考直接使用sort)

class Solution {
public:vector<int> topKFrequent(vector<int>& nums, int k) {map<int, int> num_f;for(int i = 0; i < nums.size(); i++){num_f[nums[i]]++;}vector<pair<int, int>> num_f_vec;map<int, 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(), [](pair<int, int>& a, pair<int, int>& b){return a.second > b.second;});vector<int> res;for(int i = 0; i < k; i++){res.push_back(num_f_vec[i].first);}return res;}
};
http://www.lryc.cn/news/383286.html

相关文章:

  • windchill 相关配置
  • XGBoost算法深度解析:原理、实现与应用
  • 27-29、redis优化(令牌主动失效机制)-controllert额外添加参数接收请求头、拦截器
  • 【Linux】性能分析器 gperftools 详解
  • C语言基础——函数(2)
  • Kafka Stream 流处理设计概述
  • Centos7安装自动化运维Ansible
  • element-ui 下拉菜单el-dropdown-item添加点击事件
  • Day45
  • 新媒体矩阵系统是什么?怎么搭建矩阵系统?
  • HarmonyOS应用开发——Hello World
  • Ubuntu20.04使用Samba
  • 第9章:软件可靠性基础知识
  • Go 语言学习笔记之通道 Channel
  • 第 133 场 LeetCode 双周赛题解
  • 【仿真】UR机器人相机标定、立体标定、手眼标定、视觉追踪(双目)
  • 功能测试【测试用例模板、Bug模板、手机App测试★】
  • Android音频系统
  • Android开发系列(九)Jetpack Compose之ConstraintLayout
  • SpringMVC系列三: Postman(接口测试工具)
  • 项目实训-vue(十二)
  • 达梦数据库的系统视图v$lock
  • 【无人机三维路径规划】基于树木生长算法TGA实现复杂城市地形下无人机避障三维航迹规划附Matlab代码
  • 制造业工厂的管理到底有多难
  • QTday5 2024-06-19
  • Node官网下载各个版本
  • 备战秋招day4
  • 【华为OD机试B卷】服务器广播、需要广播的服务器数量(C++/Java/Python)
  • 目标检测数据集 - 手机屏幕表面表面缺陷检测数据集下载「包含VOC、COCO、YOLO三种格式」
  • 语音相关算法学习整理