Given a string s, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string. Return the sorted string. If there are multiple answers, return any of them.

  

  考察了map的创建,map转存到vector,函数参数传递进去自定义排序(关系仿函数)这题还是比较需要基本功的。
  

class Solution {
public:
// 仿函数
typedef pair<char,int> PAIR;
struct CmpByValue{
bool operator()(const PAIR& lhs,const PAIR& rhs){
return lhs.second>=rhs.second;//
}
};
string frequencySort(string s) {
//按照频率统计字符串 第一件事情就是要提取出字符 计算频率
//哈希表进行排序?
// 处理步骤 先用map村数值 然后转存到vector 指定仿函数修改排序规则
map<char,int> dp;
for(char ss:s){
if(dp.find(ss)==dp.end()){
dp[ss]=1;
}
else{
dp[ss]++;
}
}
// 将map元素转存到vector中
vector<PAIR> dp2(dp.begin(),dp.end());
// 对vector排序
sort(dp2.begin(),dp2.end(),CmpByValue());
//根据排序后的元素拼接新的字符串
string res="";
for(auto dd:dp2){
string tmp(dd.second,dd.first);
res+=tmp;
}
return res;
}
}; 
  为了避免map中key键值重复的问题 利用优先队列(默认大顶堆)重建以出现次数为key的优先队列,减少时间复杂度。
class Solution {
public: string frequencySort(string s) {
//按照频率统计字符串 第一件事情就是要提取出字符 计算频率
//哈希表进行排序?
// 处理步骤 先用map村数值 然后转存到vector 指定仿函数修改排序规则
map<char,int> dp;
for(char ss:s){
dp[ss]++; }
//利用优先队列 大顶堆直接做
priority_queue<pair<int,char>> q;//这样避免了key不能重复的问题
string res="";
for(auto dd:dp){
q.push({dd.second,dd.first});
}
while(!q.empty()){
auto c=q.top();
string tmp(c.first,c.second);
res+=tmp;
q.pop();
} return res; }
};

  

【leetcode】451. Sort Characters By Frequency的更多相关文章

  1. 【LeetCode】451. Sort Characters By Frequency 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 优先级队列 排序 日期 题目地址:https: ...

  2. 【leetcode】905. Sort Array By Parity

    题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...

  3. 【LeetCode】排序 sort(共20题)

    链接:https://leetcode.com/tag/sort/ [56]Merge Intervals (2019年1月26日,谷歌tag复习) 合并区间 Input: [[1,3],[2,6], ...

  4. #Leetcode# 451. Sort Characters By Frequency

    https://leetcode.com/problems/sort-characters-by-frequency/ Given a string, sort it in decreasing or ...

  5. LeetCode 451. Sort Characters By Frequency (根据字符出现频率排序)

    Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...

  6. [LeetCode] 451. Sort Characters By Frequency 根据字符出现频率排序

    Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...

  7. 451. Sort Characters By Frequency

    题目: Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Inp ...

  8. 451. Sort Characters By Frequency将单词中的字母按照从高频到低频的顺序输出

    [抄题]: Given a string, sort it in decreasing order based on the frequency of characters. Example 1: I ...

  9. 【LeetCode】75. Sort Colors (3 solutions)

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

随机推荐

  1. Spring Cloud 微服务实战——nacos 服务注册中心搭建(附源码)

    作为微服务的基础功能之一的注册中心担任重要的角色.微服务将单体的服务拆分成不同的模块下的服务,而不同的模块的服务如果进行通信调用呢?这就需要服务注册与发现.本文将使用阿里开源项目 nacos 搭建服务 ...

  2. 简单理解函数声明(以signal函数为例)

    这两天遇到一些声明比较复杂的函数,比如signal函数,那我们先简单说说signal函数的用法:(参考<c陷阱与缺陷>) [signal:几乎所有c语言程序的实现过程中都要用到signal ...

  3. Spring Cloud 生产环境性能优化

    先思考几个问题: 什么是百万并发连接? 什么是吞吐量? 操作系统能否支持百万连接? 操作系统维持百万连接需要多少内存? 应用程序维持百万连接需要多少内存? 百万连接的吞吐量是否超过了网络限制? 百万的 ...

  4. asp.net中挺高性能的24种方法

    那性能问题到底该如何解决?以下是应用系统发布前,作为 .NET 开发人员需要检查的点. 1.debug=「false」 当创建 ASP.NET Web应用程序,默认设置为「true」.开发过程中,设置 ...

  5. psutil模块详解

    import psutil#1.系统性能信息模块psutilmem = psutil.virtual_memory()print(mem)#svmem(total=8442675200, availa ...

  6. pg_probackup

    [1] https://postgrespro.com/docs/enterprise/13/app-pgprobackup PITR依赖continuous WAL archiving: Makin ...

  7. [luogu7417]Minimizing Edges P

    令$e_{G}(a)$和$o_{G}(a)$分别表示在图$G$中从1到$a$的长度为奇数/偶数的最短路(若该类最短路不存在则为$\infty$),不难得到有以下结论--$f_{G}(a,b)=\beg ...

  8. 一文详解MySQL的锁机制

    一.表级锁.行级锁.页级锁 数据库锁定机制简单来说,就是数据库为了保证数据的一致性,而使各种共享资源在被并发访问变得有序所设计的一种规则. MySQL数据库由于其自身架构的特点,存在多种数据存储引擎, ...

  9. docker详细

    镜像(image) 容器(container) 启动,删除,停止 仓库(repository)   docker images  

  10. k8s statefulset controller源码分析

    statefulset controller分析 statefulset简介 statefulset是Kubernetes提供的管理有状态应用的对象,而deployment用于管理无状态应用. 有状态 ...