https://leetcode.com/problems/sort-characters-by-frequency/

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

Example 1:

Input:
"tree" Output:
"eert" Explanation:
'e' appears twice while 'r' and 't' both appear once.
So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.

Example 2:

Input:
"cccaaa" Output:
"cccaaa" Explanation:
Both 'c' and 'a' appear three times, so "aaaccc" is also a valid answer.
Note that "cacaca" is incorrect, as the same characters must be together.

Example 3:

Input:
"Aabb" Output:
"bbAa" Explanation:
"bbaA" is also a valid answer, but "Aabb" is incorrect.
Note that 'A' and 'a' are treated as two different characters.

代码:

class Solution {
public:
string frequencySort(string s) {
string ans = "";
int len = s.length();
unordered_map<char, int> mp;
priority_queue<pair<int, char> > q; for(int i = 0; i < len; i ++)
mp[s[i]] ++; for(auto i : mp)
q.push({i.second, i.first}); while(!q.empty()) {
int k = q.top().first;
char c = q.top().second;
for(int i = 0; i < k; i ++)
ans += c;
q.pop();
} return ans;
}
};

  优先队列

明天就肥家放假啦

#Leetcode# 451. Sort Characters By Frequency的更多相关文章

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

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

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

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

  3. 【leetcode】451. Sort Characters By Frequency

    Given a string s, sort it in decreasing order based on the frequency of the characters. The frequenc ...

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

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

  5. 451. Sort Characters By Frequency

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

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

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

  7. 451. Sort Characters By Frequency (sort map)

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

  8. [LC] 451. Sort Characters By Frequency

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

  9. 451. Sort Characters By Frequency(桶排序)

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

随机推荐

  1. Oracle的 EXEC SQL CONTEXT学习

    磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL杂记页     回到顶级页面:PostgreSQL索引页 [作者 高健@博客园  luckyjackgao@gmail. ...

  2. DELL R710使用4T硬盘亮黄灯

    事件背景 公司DELL R710的物理机上面运行的SQL SERVER数据库,因存储空间不足需要扩充空间.现系统盘(300G SAS 6Gbps 15K*2)RAID 1,数据盘(500G SAS 6 ...

  3. .net core中automapper的使用

    automapper 是将两个类中的相同字段进行映射,也可以指定字段进行映射:将 UserDao的id 映射为 User 的age CreateMap<UserDao, User>() . ...

  4. 博弈论简单入门sb总结

    博弈论简单入门sb总结 下午讲博弈论.没预习,GG. 整个下午都在学. 0 有一堆共n个石子,两个人轮流取石子,每个人一次可以取1到k个,取到最后一个石子的人胜利. 小学生都会的sb题.若k+1|n, ...

  5. luogu1117 [NOI2016]优秀的拆分

    luogu1117 [NOI2016]优秀的拆分 https://www.luogu.org/problemnew/show/P1117 后缀数组我忘了. 此题哈希可解决95分(= =) 设\(l_i ...

  6. nginx Location正则表达式

    1. Location正则表达式 1.1. location的作用 location指令的作用是根据用户请求的URI来执行不同的应用,也就是根据用户请求的网站URL进行匹配,匹配成功即进行相关的操作. ...

  7. Linux(centos7)之更换安装python3(二)

    Linux不比window好安装python,折腾了好久,终于成功安装上了python,window上一个安装包完事,可惜Linux上python版本太低不好使,还要更换为3版本,百度了好久,教程上总 ...

  8. jquery 3.0 新版本

    https://code.jquery.com/

  9. 关于linux-centos7 安装完成git后npm突然无法使用问题处理

    报错: 解决方法: 查看一下nodejs是否安装,如果没有安装的话安装完成就能解决了

  10. groupadd命令详解

    基础命令学习目录首页 原文链接:https://wtj6891.iteye.com/blog/2096076 groupadd创建组群 使用groupadd命令可以在系统中创建组群账户 语法: gro ...