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

C++ | Leetcode C++题解之第451题根据字符出现频率排序

题目:

题解:

class Solution {
public:string frequencySort(string s) {unordered_map<char, int> mp;int maxFreq = 0;int length = s.size();for (auto &ch : s) {maxFreq = max(maxFreq, ++mp[ch]);}vector<string> buckets(maxFreq + 1);for (auto &[ch, num] : mp) {buckets[num].push_back(ch);}string ret;for (int i = maxFreq; i > 0; i--) {string &bucket = buckets[i];for (auto &ch : bucket) {for (int k = 0; k < i; k++) {ret.push_back(ch);}}}return ret;}
};

http://www.mrgr.cn/news/40922.html

相关文章:

  • Linux 实用工具Axel安装及使用教程(支持多线程下载)
  • SD-WebUI forge支持flux模型。算力互联forge镜像使用教程
  • Unity Input System自动生成配置
  • AccessoriesqueryController
  • Difference of Convex Relaxation (DC)
  • 基于深度学习的任务序列中的快速适应
  • Hive数仓操作(十)
  • 课设实验-数据结构-线性表-手机销售
  • 2024多模态大模型发展调研
  • buuctf--->Youngter-drive
  • 复习HTML(基础)
  • 重置linux后vscode无法再次使用ssh连接
  • 【ShuQiHere】深入理解 LC-3 指令集架构(LC-3 ISA):硬件与软件的桥梁 ️
  • 华为OD机试真题---数大雁
  • 在Ubuntu 14.04上安装带SSL的Webmin的方法
  • profile-spec-ref元素
  • 【计算机毕业设计】springboot企业客户信息反馈平台
  • Linux基础命令parted详解
  • 【简介Sentinel-1】
  • 【1】野火STM32F103VET6开发板入门笔记之点亮RGB