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.
class Solution {
public:
//桶排序。将相同长度的子串放到同一个桶中,最后利用桶排序
string frequencySort(string s) {
     //防止s="aaa" 插入 b[3] = "aaa";
vector<string> bucket(s.size()+1,"");
map<char,int> m;
string res;
for(int i=0;i<s.size();i++){
m[s[i]]++;
}
for(auto sub:m){
int n = sub.second;
int c = sub.first;
//长度为n的子串都放到bucket[n]
bucket[n].append(string(n,c));
}
//最后利用桶排序,倒序遍历桶即可
for(int i=bucket.size()-1;i>=0;i--){
res.append(bucket[i]);
}
return res;
}
};


451. Sort Characters By Frequency(桶排序)的更多相关文章

  1. 【leetcode】451. Sort Characters By Frequency

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

  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, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...

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

    给定一个字符串,请将字符串里的字符按照出现的频率降序排列.示例 1:输入:"tree"输出:"eert"解释:'e'出现两次,'r'和't'都只出现一次.因此' ...

  5. 451. Sort Characters By Frequency

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

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

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

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

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

  8. #Leetcode# 451. Sort Characters By Frequency

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

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

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

随机推荐

  1. JProfiler 教程 使用说明

    JProfiler    (本文原创转载请注明) 简介 JProfiler是一个重量级的JVM监控工具,提供对JVM精确监控,其中堆遍历.CPU剖析.线程剖析看成定位当前系统瓶颈的得力工具.可以统计压 ...

  2. tomcat 验证码显示问题

    在Web开发中使用验证码时可能遇到的问题:java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsE ...

  3. 多测师讲解jmeter _安装和配置环境(00)_高级讲师肖sir

    1.下载jmeter包,我们已经下载了有现成的: 2.安装jjdk默认安装或自定义安装 默认安装的路径: 如下图 3.第三步:安装完成后配置JDK的环境变量  位置:计算机→属性→高级系统设置→高级→ ...

  4. RLP序列化算法

    RLP RLP(Recursive Length Prefix)递归长度前缀编码,是由以太坊提出的序列化/反序列化标准,相比json格式体积更小,相比protobuf对多语言的支持更强. RLP将数据 ...

  5. DateDiff() 方法语法 T-SQL语法

    表达式DateDiff(timeinterval,date1,date2 [, firstdayofweek [, firstweekofyear]]) 允许数据类型: timeinterval 表示 ...

  6. 白话k8s-Pod的组成

    k8s的所有功能都是围绕着Pod进行展开的,我们经常会看到类似这样一张图 告诉我们,Pod是一组container的集合,container之间可以通过localhost:port的方式直接访问. 感 ...

  7. C# 将Excel里面的数据填充到DataSet中

    /// <summary> /// 将Excel表里的数据填充到DataSet中 /// </summary> /// <param name="filenam ...

  8. nio DirectByteBuffer如何回收堆外内存

    概述 使用了nio框架的应用,比如服务框架,利用nio建立长连接通信,他们会使用DirectByteBuffer来分配堆外内存,也就是本地直接内存,这个内存的回收不由gc直接维护,我们通常所说的gc, ...

  9. better-scroll插件 api

    Vue中的better-scroll插件 在需要的文件中添加 import BScorll from 'better-scroll'; 引用的示例代码: let scroll = new BScrol ...

  10. Albert学习记录

    albert相对BERT而言主要有三个改进方向: 1.对Embedding因式分解 在BERT中,词向量维度E和隐层维度H是相等的.而词嵌入学习的是单词与上下文无关的表示,而隐层则是学习与上下文相关的 ...