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.

Approach #1: c++.

class Solution {
public:
string frequencySort(string s) {
unordered_map<char, int> mp;
string ans = "";
for (int i = 0; i < s.length(); ++i) {
mp[s[i]]++;
}
vector<pair<char, int>> v(mp.begin(), mp.end());
sort(v.begin(), v.end(), cmp);
for (int i = 0; i < v.size(); ++i) {
while (v[i].second--) {
ans += v[i].first;
}
}
return ans;
} private:
static bool cmp(pair<char, int> a, pair<char, int> b) {
return a.second > b.second;
}
};

  

451. Sort Characters By Frequency (sort map)的更多相关文章

  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 s, sort it in decreasing order based on the frequency of the characters. The frequenc ...

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

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

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

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

  5. [LC] 451. Sort Characters By Frequency

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

  6. 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. #Leetcode# 451. Sort Characters By Frequency

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

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

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

随机推荐

  1. EasyPusher/EasyDarwin/EasyPlayer实现手机直播版本及效果整理

    EasyPusher手机直播 实现功能 最近很多EasyDarwin爱好者提出了手机移动端直播的功能需求,尤其是如何做出像映客这样能够快速出画面播放的效果,经过一段时间的移动端和服务端的优化,Easy ...

  2. WildFly JBoss 应用程序服务器

    https://en.wikipedia.org/wiki/WildFly [实现基于面向服务的架构SOA的web应用和服务] WildFly,[1] formerly known as JBoss ...

  3. mongodb学习之:GridFS

    GridFS是一种在Mongodb中存储大二进制文件的机制.GridFS 用于存储和恢复那些超过16M(BSON文件限制)的文件(如:图片.音频.视频等). 使用GridFS有如下几个原因: 1 利用 ...

  4. Python pandas 获取Excel重复记录

    pip install pandas pip install xlrd 大量记录的时候,用EXCEL排序处理比较费劲,EXCEL程序动不动就无响应了,用pands完美解决. # We will use ...

  5. 关于Fragment的onActivityResult 不执行

    1.getActivity().startActivityForResult();  与 fragment.startActivityForActivity(): getActivity().star ...

  6. ThinkPHP RBAC权限管理机制

    RBAC是ThinkPHP很好用的后台权限管理的,话不多说,实现方法如下,也方便以后自己查询使用: 1.新建4个数据库表 self_role权限表 CREATE TABLE `self_role` ( ...

  7. redis一些笔记

    base 字典: hget/hset 在redis字典中值只能是字符串,使用渐进式进行rehash.在rehash的过程中,会保留两个hash结构:查询时会同时查询两个结构:逐渐完成hash的迁移. ...

  8. 查看ubuntu磁盘空间占用及占用空间大的文件

    最近老是收到 ecs上有台服务器的磁盘利用率高 终于有一天 ssh登不上去了 http://blog.csdn.net/aaashen/article/details/50685988 清除相关大文件 ...

  9. window上安装rabbitMQ

    win7下安装RabbitMQ http://my.oschina.net/ydsakyclguozi/blog/528835?fromerr=q7m1OxxF 前辈总结的特别详细.

  10. nginx启动不了

    nginx简介 Nginx是一个高性能的HTTP和反向代理服务器. 支持的操作系统众多,windows.linux. MacOS X: 可实现负载均衡: Rewrite功能强大: 电商架构大部分都采用 ...