【LeetCode】451. Sort Characters By Frequency 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/sort-characters-by-frequency/description/
题目描述
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.
题目大意
很喜欢这种题目很短,而栗子很多的题目~
题意就是把字符串按照字符出现的次数重新排列。
解题方法
字典
用python真的超级简单呀使用Counter类就能统计每个字符出现的次数,使用most_common函数就能按次序排列,最后字符与其出现的次数相乘就得到了字符串
下面是使用的一个例子的结果:
count = collections.Counter('abbdfas').most_common()
print count
# 输出
[('a', 2), ('b', 2), ('s', 1), ('d', 1), ('f', 1)]
代码:
class Solution(object):
def frequencySort(self, s):
"""
:type s: str
:rtype: str
"""
count = collections.Counter(s).most_common()
res = ''
for c, v in count:
res += c * v
return res
优先级队列
C++默认的priority_queue是大顶堆。
C++构造把字符重复多次的新字符串的一个方法是append方法,第一个参数是整形表示重复次数,第二个参数是字符。
C++代码如下:
class Solution {
public:
string frequencySort(string s) {
string res;
unordered_map<char, int> count;
for (char c : s) count[c]++;
priority_queue<pair<int, char>> q;
for (auto a : count) q.push({a.second, a.first});
while (!q.empty()) {
auto t = q.top(); q.pop();
res.append(t.first, t.second);
}
return res;
}
};
排序
参考Grandyang的解法:http://www.cnblogs.com/grandyang/p/6231504.html
我们也可以使用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;
}
};
日期
2018 年 3 月 4 日
2018 年 12 月 11 日 —— 双十一已经过去一个月了,真快啊。。
【LeetCode】451. Sort Characters By Frequency 解题报告(Python & C++)的更多相关文章
- #Leetcode# 451. Sort Characters By Frequency
https://leetcode.com/problems/sort-characters-by-frequency/ Given a string, sort it in decreasing or ...
- 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 ...
- 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: ...
随机推荐
- nginx_install
[root@MiWiFi-R1CM-srv ~]# yum install -y gcc-c++ zlib zlib-devel openssl openssl-devel pcre-devel pc ...
- rabbit mq的php使用 php-amqplib操作消息队列
rabbit mq的php使用 php-amqplib操作消息队列 有大神翻译的教程,非常清楚可以参考: https://xiaoxiami.gitbook.io/rabbitmq_into_chin ...
- 44-Count and Say
Count and Say My Submissions QuestionEditorial Solution Total Accepted: 79863 Total Submissions: 275 ...
- Oracle-连接多个字段
0.函数concat(A,B)作用:链接字符串 区别: Oracle中:CONCAT()只允许两个参数:(貌似可以内嵌) Mysql中:CONCAT()可以连接多个参数: 1.用||符号进行拼接 例子 ...
- 日常Java 2021/10/4
读取控制台输入 将System.in包装在BufferedReader对象中来创建一个字符流 BufferedReader b = new BufferedReader(new InputStream ...
- 游戏案例|Service Mesh 在欢乐游戏的应用演变和实践
作者 陈智伟,腾讯 12 级后台专家工程师,现负责欢乐游戏工作室公共后台技术研发以及团队管理工作.在微服务分布式架构以及游戏后台运维研发有丰富的经验. 前言 欢乐游戏工作室后台是分布式微服务架构,目前 ...
- Redis6 新特性
Redis6新特性 ACL安全策略 ACL(access control list): 访问控制列表,可以设置多个用户,并且给每个用户单独设置命令权限和数据权限 default用户和使用require ...
- 多人协作解决方案,git flow的使用
简介 Gitflow工作流程围绕项目发布定义了严格的分支模型. 为不同的分支分配了非常明确的角色,并且定义了使用场景和用法.除了用于功能开发的分支,它还使用独立的分支进行发布前的准备.记录以及后期维护 ...
- openwrt装载固件
方法1. 确定串口号以后(在设备管理器可以查看) 打开SecureCRT软件,选择串口,设置合适的波特率(我用的115200),然后快速连接, 板子通电启动,在启动的时候会提示按任意键中断,这时按下任 ...
- Kotlin 学习(2)
属性和字段 1.声明属性 Kotlin中可以使用var关键字声明可变属性,或者用val关键字声明只读属性,属性的类型在后面,变量名在前面,中间加冒号和空格. public class Address ...