[LeetCode] 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.
这道题让我们给一个字符串按照字符出现的频率来排序,那么毫无疑问肯定要先统计出每个字符出现的个数,那么之后怎么做呢?我们可以利用优先队列的自动排序的特点,把个数和字符组成pair放到优先队列里排好序后,再取出来组成结果res即可,参见代码如下:
解法一:
class Solution {
public:
string frequencySort(string s) {
string res = "";
priority_queue<pair<int, char>> q;
unordered_map<char, int> m;
for (char c : s) ++m[c];
for (auto a : m) q.push({a.second, a.first});
while (!q.empty()) {
auto t = q.top(); q.pop();
res.append(t.first, t.second);
}
return res;
}
};
我们也可以使用STL自带的sort来做,关键就在于重写comparator,由于需要使用外部变量,记得中括号中放入&,然后我们将频率大的返回,注意一定还要处理频率相等的情况,要不然两个频率相等的字符可能穿插着出现在结果res中,这样是不对的。参见代码如下:
解法二:
class Solution {
public:
string frequencySort(string s) {
unordered_map<char, int> m;
for (char c : s) ++m[c];
sort(s.begin(), s.end(), [&](char& a, char& b){
return m[a] > m[b] || (m[a] == m[b] && a < b);
});
return s;
}
};
我们也可以不使用优先队列,而是建立一个字符串数组,因为某个字符的出现次数不可能超过s的长度,所以我们将每个字符根据其出现次数放入数组中的对应位置,那么最后我们只要从后往前遍历数组所有位置,将不为空的位置的字符串加入结果res中即可,参见代码如下:
解法三:
class Solution {
public:
string frequencySort(string s) {
string res;
vector<string> v(s.size() + );
unordered_map<char, int> m;
for (char c : s) ++m[c];
for (auto &a : m) {
v[a.second].append(a.second, a.first);
}
for (int i = s.size(); i > ; --i) {
if (!v[i].empty()) res.append(v[i]);
}
return res;
}
};
类似题目:
First Unique Character in a String
参考资料:
https://leetcode.com/problems/sort-characters-by-frequency/description/
https://leetcode.com/problems/sort-characters-by-frequency/discuss/93404/c-on-solution-without-sort
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 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: ...
- 451 Sort Characters By Frequency 根据字符出现频率排序
给定一个字符串,请将字符串里的字符按照出现的频率降序排列.示例 1:输入:"tree"输出:"eert"解释:'e'出现两次,'r'和't'都只出现一次.因此' ...
- Leetcode: Sort Characters By Frequency
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
- Leetcode 451.根据字符出现频率排序
根据字符出现频率排序 给定一个字符串,请将字符串里的字符按照出现的频率降序排列. 示例 1: 输入: "tree" 输出: "eert" 解释: 'e'出现两次 ...
- Java实现 LeetCode 451 根据字符出现频率排序
451. 根据字符出现频率排序 给定一个字符串,请将字符串里的字符按照出现的频率降序排列. 示例 1: 输入: "tree" 输出: "eert" 解释: 'e ...
- 【leetcode】451. Sort Characters By Frequency
Given a string s, sort it in decreasing order based on the frequency of the characters. The frequenc ...
- 给定一个字符串,根据字符出现频率排序--Java实现
题目描述: 给定一个字符串,请将字符串里的字符按照出现的频率降序排列. 示例 1: 输入:"tree" 输出:"eert" 解释:'e'出现两次,'r'和't' ...
- LeetCode 451. Sort Characters By Frequency (根据字符出现频率排序)
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
- [Swift]LeetCode451. 根据字符出现频率排序 | Sort Characters By Frequency
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
随机推荐
- Oracle 11g静默安装简明版
环境:RHEL 6.5 + Oracle 11.2.0.4 1. 初步处理应答文件 2. 静默安装软件 3. 静默安装监听 4. 静默dbca建库 说明: 本文默认安装软件前的步骤已经设置完毕 如果没 ...
- 分布式系统理论基础 - CAP
引言 CAP是分布式系统.特别是分布式存储领域中被讨论最多的理论,“什么是CAP定理?”在Quora 分布式系统分类下排名 FAQ 的 No.1.CAP在程序员中也有较广的普及,它不仅仅是“C.A.P ...
- 深入学习jQuery特性操作
× 目录 [1]获取特性 [2]设置特性 [3]删除特性 前面的话 每个元素都有一个或者多个特性,这些特性的用途就是给出相应元素或者其内容的附加信息.操作特性的DOM方法主要有3个:getAttrib ...
- DOM 事件深入浅出(一)
在项目开发时,我们时常需要考虑用户在使用产品时产生的各种各样的交互事件,比如鼠标点击事件.敲击键盘事件等.这样的事件行为都是前端DOM事件的组成部分,不同的DOM事件会有不同的触发条件和触发效果.本文 ...
- 扩展方法(C#)
扩展方法使你能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型.扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用. 下面的示例为String添加 ...
- 【QuickHit项目实例】
关于java面向对象QuickHit项目实例 Game类:用来得到随机出现的字符串(随机打印的字符串,然后玩家进行输入字符串) public class Game { private String s ...
- APNS 远程推送通知 PUSH deviceToken
服务器向客户端推送消息: 当应用程序推到后台,或者根本就没有运行(我们的代码无能为力) 如果这种情况之下,应用程序想和用户交互(传统的做法 不可能) 推送 APNS:Ap ...
- Basic Tutorials of Redis(3) -Hash
When you first saw the name of Hash,what do you think?HashSet,HashTable or other data structs of C#? ...
- WinForm拖动没有标题栏窗体的方法
建立窗体的名称修改为:Form_HoverTree 文后附有源码下载. 主要代码: Point _HoverTreePosition; public Form_HoverTree() { Initia ...
- 配置 EPEL yum 源
当我们在linux上, 使用yum 安装包时,报错如下: Loaded plugins: product-id, refresh-packagekit, security, subscription- ...