#Leetcode# 451. Sort Characters By Frequency
https://leetcode.com/problems/sort-characters-by-frequency/
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.
代码:
class Solution {
public:
string frequencySort(string s) {
string ans = "";
int len = s.length();
unordered_map<char, int> mp;
priority_queue<pair<int, char> > q; for(int i = 0; i < len; i ++)
mp[s[i]] ++; for(auto i : mp)
q.push({i.second, i.first}); while(!q.empty()) {
int k = q.top().first;
char c = q.top().second;
for(int i = 0; i < k; i ++)
ans += c;
q.pop();
} return ans;
}
};
优先队列
明天就肥家放假啦
#Leetcode# 451. Sort Characters By Frequency的更多相关文章
- LeetCode 451. Sort Characters By Frequency (根据字符出现频率排序)
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
- [LeetCode] 451. Sort Characters By Frequency 根据字符出现频率排序
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
- 【leetcode】451. Sort Characters By Frequency
Given a string s, sort it in decreasing order based on the frequency of the characters. The frequenc ...
- 【LeetCode】451. Sort Characters By Frequency 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 优先级队列 排序 日期 题目地址:https: ...
- 451. Sort Characters By Frequency
题目: Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Inp ...
- 451. Sort Characters By Frequency将单词中的字母按照从高频到低频的顺序输出
[抄题]: Given a string, sort it in decreasing order based on the frequency of characters. Example 1: I ...
- 451. Sort Characters By Frequency (sort map)
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
- [LC] 451. Sort Characters By Frequency
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
- 451. Sort Characters By Frequency(桶排序)
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
随机推荐
- 简单的贝叶斯分类器的python实现
# -*- coding: utf-8 -*- ''' >>> c = Classy() >>> c.train(['cpu', 'RAM', 'ALU', 'io ...
- 洛谷P1313 计算系数【快速幂+dp】
P1313 计算系数 题目描述 给定一个多项式(by+ax)^k,请求出多项式展开后x^n*y^m 项的系数. 输入输出格式 输入格式: 输入文件名为factor.in. 共一行,包含5 个整数,分别 ...
- 大话 .Net 之内存管理
在一次偶然的机会中,我来到了恒生的大家庭.又在一次偶然的机会中,我很荣幸的被勇哥信任并让我写一篇季刊的文章.可能人生之中充满了无数次的偶然机会,我们只有抓住眼前的“偶然”,才可以创建人生.当我接到这个 ...
- jmeter汉化
测试需要许多工具辅助工作,我们在使用这些工具之前需要对工具有一定的了解,第一步就是需要安装特定的软件. 因为工具基本上都是英文版的,所以菜鸟使用就面临汉化的问题,下面介绍几种汉化的办法: 一.万事不决 ...
- jenkins使用时出现的问题!
从安装到日常使用中遇到过的问题和解决方法: 背景/问题:安装时是跳过安装插件过程的,安装好后,我发现里面啥也做不了,连个git的插件都无法下载. 方法:在jenkins的主界面,打开系统管理= ...
- LintCode——尾部的零
尾部的零:设计一个算法,计算出n阶乘中尾部零的个数 样例:11! = 39916800.因此应该返回2 分析:假如你把1 × 2 ×3× 4 ×……×N中每一个因数分解质因数,例如 1 × 2 × 3 ...
- python类与对象的组合与继承
1.把类的对象实例化放到一个新的类里面叫做类的组合,组合就是指几个横向关系的类放在一起,纵向关系的类放在一起是继承,根据实际应用场景确定.简单的说,组合用于“有一个”的场景中,继承用于“是一个”的场景 ...
- c语言数字图像处理(十):阈值处理
定义 全局阈值处理 假设某一副灰度图有如下的直方图,该图像由暗色背景下的较亮物体组成,从背景中提取这一物体时,将阈值T作为分割点,分割后的图像g(x, y)由下述公式给出,称为全局阈值处理 多阈值处理 ...
- Laya自动图集原理
关于Laya自动图集 Laya会把size小于512*512的图片打入自动大图集中.如果图片被打入自动图集中,图片的内存就交由Laya自动处理,开发者不能手动删除. Laya最多生成6张2048*20 ...
- nginx 定义的一些状态码
ngx_string(ngx_http_error_494_page), /* 494, request header too large */ ngx_string(ngx_http_erro ...