题目:

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.

分析:

给定一个字符串,请将字符串里的字符按照出现的频率降序排列。

基本思路就是遍历一遍字符串,将字符出现的次数存进Hashmap中,再遍历一遍Hashmap将字符和出现次数作为一组pair存进优先级队列中,再从队列中依次取出数据拼接成最后的字符串。

程序:

C++

class Solution {
public:
string frequencySort(string s) {
unordered_map<char, int> m;
for(const auto& c:s)
m[c]++;
priority_queue <pair<char, int>, vector<pair<char, int>>, cmp >q;
for(const auto& i:m){
q.push(make_pair(i.first, i.second));
}
string res = "";
while(!q.empty()){
res.append(q.top().second, q.top().first);
q.pop();
}
return res;
}
private:
struct cmp{
bool operator() (pair<char, int> a, pair<char, int> b)
{
return a.second < b.second;
}
};
};

Java

class Solution {
public String frequencySort(String s) {
for(char c:s.toCharArray()){
map.put(c, map.getOrDefault(c,0) + 1);
}
p.addAll(map.entrySet());
while(!p.isEmpty()){
Map.Entry<Character, Integer> e = p.poll();
for(int i = 0; i < e.getValue().intValue(); i++){
res.append(e.getKey());
}
}
return res.toString();
}
private StringBuilder res = new StringBuilder();
private HashMap<Character, Integer> map = new HashMap<>();
private PriorityQueue<Map.Entry<Character, Integer>> p = new PriorityQueue<>(new Comparator<Map.Entry<Character, Integer>>()
{
public int compare(Map.Entry<Character, Integer> a, Map.Entry<Character, Integer> b)
{
return b.getValue() - a.getValue();
}
});
}

LeetCode 451. Sort Characters By Frequency 根据字符出现频率排序 (C++/Java)的更多相关文章

  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. 451 Sort Characters By Frequency 根据字符出现频率排序

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

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

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

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

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

  6. 【leetcode】451. Sort Characters By Frequency

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

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

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

  8. 451. Sort Characters By Frequency

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

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

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

  10. 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. 【笔记】oracle INTERSECT指令&邮箱的正则匹配&trim()函数

    [笔记]oracle INTERSECT 和 UNION 指令类似, INTERSECT 也是对两个 SQL 语句所产生的结果做处理的. 不同的地方是, UNION 基本上是一个 OR (如果这个值存 ...

  2. 龙湖千丁基于 ACK@Edge 的云原生智慧停车系统架构实践

    简介: 结合龙湖千丁自研的新版停车云系统以及 ACK@Edge 提供的标准 Kubernetes 服务以及云边一体化协同解决方案,整体来着,边缘部署时间成本由 1 天缩短到 3 小时,将之前的手动升级 ...

  3. 动态尺寸模型优化实践之Shape Constraint IR Part II

    简介: 在本系列分享中我们将介绍BladeDISC在动态shape语义下做性能优化的一些实践和思考.本次分享的是我们最近开展的有关shape constraint IR的工作,Part II 中我们将 ...

  4. EasyNLP带你玩转CLIP图文检索

    简介: 本文简要介绍CLIP的技术解读,以及如何在EasyNLP框架中玩转CLIP模型. 作者:熊兮.章捷.岑鸣.临在 导读 随着自媒体的不断发展,多种模态数据例如图像.文本.语音.视频等不断增长,创 ...

  5. 云效Codeup代码评审中的代码协同

    简介: 云效 Codeup 汇集了阿里巴巴最新的代码托管.代码协同技术,希望能够造福更多中国和世界的开发者. 大神说:"Show me the code",于是就有了代码评审. & ...

  6. 揭秘远程证明架构EAA:机密容器安全部署的最后一环 | 龙蜥技术

    ​简介:如果需要在云上 HW-TEE 环境里启动一个加密容器,如何在启动过程中获取容器的解密密钥? ​ 文 / 周亮, 云原生机密计算 SIG 核心成员. 在云原生场景下,基于HW-TEE(如Inte ...

  7. [ML] 数据预处理 - 特性归一化的目的

    简而言之,归一化的目的就是使得预处理的数据被限定在一定的范围内(比如[0,1]或者[-1,1]), 从而消除奇异样本数据导致的不良影响. 是否归一化主要在于是否关心变量取值. Tool:ChatAI ...

  8. 2019-7-2-WPF-通过-ReadyToRun-提升性能

    title author date CreateTime categories WPF 通过 ReadyToRun 提升性能 lindexi 2019-07-02 10:29:55 +0800 201 ...

  9. K8s控制器---Replicaset(7)

    一.Replicaset(目前少用了) 1.1 控制器管理pod 什么是控制器?前面我们学习了 Pod,那我们在定义 pod 资源时,可以直接创建一个 kind:Pod 类型的自主式 pod,但是这存 ...

  10. linux Makefile 如何将生成的 .o 文件放到指定文件夹

    一.Makefile文件 为了方便分析,直接上文件,Makefile 文件中的内容如下所示: # # Makefile # 编译的.o文件和.c文件在同一路径下 # $(info "star ...