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. JNI在C和C++中的调用区别

    C-style JNI looks like (*env)->SomeJNICall(env, param1 ...) C++ style JNI looks like env->Some ...

  2. 关于wpf中popup跟随鼠标移动显示

    最近在做一个画图工具,里面有一个功能是需要实现,当鼠标移动的时候在,鼠标的旁边显示坐标信息. 第一反应是想到了tooltip,但是tooltip有许多的限制,查询资料的过程中看到了popup,popu ...

  3. 【windows phone】CollectionViewSource的妙用

    在windows phone中绑定集合数据的时候,有时候需要分层数据,通常需要以主从试图形式显示.通常的方法是将第二个ListBox(主视图)的数据源绑定到第一个ListBox (从视图)的Selec ...

  4. UML类图组成

    本文转载至 http://blog.csdn.net/fengsh998/article/details/8105666     UML类图的相关知识,UML类图(Classdiagram)是最常用的 ...

  5. EasyDarwin开源手机直播方案:EasyPusher手机直播推送,EasyDarwin流媒体服务器,EasyPlayer手机播放器

    在不断进行EasyDarwin开源流媒体服务器的功能和性能完善的同时,我们也配套实现了目前在安防和移动互联网行业比较火热的移动端手机直播方案,主要就是我们的 EasyPusher直播推送项目 和 Ea ...

  6. js 怎么传递参数

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=&qu ...

  7. Apache JServ Protocol

    ajp_百度百科 https://baike.baidu.com/item/ajp/1187933 AJP(Apache JServ Protocol)是定向包协议.因为性能原因,使用二进制格式来传输 ...

  8. BZOJ2759: 一个动态树好题

    BZOJ2759: 一个动态树好题 Description 有N个未知数x[1..n]和N个等式组成的同余方程组:x[i]=k[i]*x[p[i]]+b[i] mod 10007其中,k[i],b[i ...

  9. Dispatch Sources(转)

    Dispatch Sources 现代系统通常提供异步接口,允许应用向系统提交请求,然后在系统处理请求时应用可以继续处理自己的事情.Grand Central Dispatch正是基于这个基本行为而设 ...

  10. 配置composer代理

    composer config -g repo.packagist composer https://packagist.phpcomposer.com