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. switch中的case不加break执行情况

    输出结果:230 分析,switch先匹配一个case满足$a,然后执行case里面的语句,直到遇到break,否则一直往下执行 <?php $a = ; switch($a){ : echo ...

  2. ASP.NET车辆管理系统100%源代码

    系统开发环境为VS2010.採用ASP.NET框架.数据库採用SQL Server.系统採用Ajax,具有:GPS导航(实时监控报警).申请审核.流程查看及短信息发送等功能.这个系统界面和功能是我认为 ...

  3. HDU 6074 Phone Call LCA + 并查集

    Phone Call Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Pro ...

  4. 开源流媒体播放器EasyPlayer

    配套开源流媒体服务器EasyDarwin,我们开发了一款开源的流媒体播放器EasyPlayer(现在已经升级合并到开源EasyClient中):同样,EasyPlayer目前只支持RTSP流媒体协议, ...

  5. HDOJ 4704 Sum 规律 欧拉定理

    规律 欧拉定理: 找规律 2^n-1 ,n 非常大用欧拉定理 Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/13 ...

  6. 用css3技术给网站加分

    自己写了几个小DEMO,请打开http://codepen.io/shenggen/

  7. Mongoose学习(3)--设置环境变量

    比如我一套代码数据库代码分为中文站和英文站,每个表中我都有一个site_code字段来区分, 两个站点部署在不同的人服务器,这个时候我们就用系统环境变量来区分, 下面直接在mac下设置环境变量 vim ...

  8. Redis C语言操作封装

    #ifndef BOYAA_FOURLANDLORD_REDISCLASS_H_20130217 #define BOYAA_FOURLANDLORD_REDISCLASS_H_20130217 #i ...

  9. Codeforces Round #376 (Div. 2) D. 80-th Level Archeology —— 差分法 + 线段扫描法

    题目链接:http://codeforces.com/contest/731/problem/D D. 80-th Level Archeology time limit per test 2 sec ...

  10. Appium——adb 启动问题Invalid argument: cannot open transport registration socketpair could not read ok from ADB Server failed to start daemon * error: cannot connect to daemon

    adb启动问题:Invalid argument: cannot open transport registration socketpair could not read ok from ADB S ...