LeetCode 451. 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.
题目标签:Hash Table | Heap
题目给了我们一个string s, 让我们把s 按照每一个char 的出现频率从大到小排序。
首先遍历string s,把每一个char 当作 key, 出现次数当作 value 存入 map;
接着利用priorityQueue,按照 value 从大到小的 顺序 排列,把map 的data 存入 pq;
遍历 pq,把每一个char 按照 它的value 组成对应长度 string 存入 res。
Java Solution:
Runtime beats 40.06%
完成日期:06/23/2017
关键词:HashMap; Priority Queue
关键点:把Map 存入 pq
class Solution
{
public String frequencySort(String s)
{
HashMap<Character, Integer> map = new HashMap<>();
StringBuilder res = new StringBuilder(); // store s char and frequency into map
for(char c: s.toCharArray())
map.put(c, map.getOrDefault(c, 0) + 1); // set up priorityQueue in value descending order
PriorityQueue<Map.Entry<Character, Integer>> pq = 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();
}
}
); // add map into priorityQueue
pq.addAll(map.entrySet()); // iterate pg, add each char with value times into res
while(!pq.isEmpty())
{
Map.Entry<Character, Integer> entry = pq.poll(); for(int i=0; i<(int) entry.getValue(); i++)
res.append(entry.getKey());
} return res.toString();
}
}
参考资料:
https://discuss.leetcode.com/topic/66024/java-o-n-bucket-sort-solution-o-nlogn-priorityqueue-solution-easy-to-understand
LeetCode 题目列表 - LeetCode Questions List
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: ...
- 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. 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 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: ...
随机推荐
- python __slots__ 详解(上篇)
转自:http://blog.csdn.net/sxingming/article/details/52892640 python中的new-style class要求继承Python中的一个内建类型 ...
- jsp学习笔记 - 内置对象 pageContext
1.pageContext几乎可以操作所有的页面内置对象 pageContext.getRequest(); 得到的对象只是属于ServletRequest类,httpServletReques ...
- 【笔记JS/HTML/CSS】ubuntu环境下的sublime text2 安装 zenCoding
刚接触web编程的时候就被老师安利了sublime text2 这个文本编辑器,后来发现它真的挺好用的,无论是windows还是ubuntu,都可以很简单地下载安装(到官网,免费哦),三分钟内就搞定了 ...
- 安卓app测试之内存监控
一.通过Dumpsys 来取值 1.adb shell dumpsys meminfo 获取的所有进程的内存信息,以及总内存,剩余内存,使用的内存等信息. 2.想获得某一进程内存的详细信息,在后面加上 ...
- IntelliJ IDEA 2017安装和破解方法
一,安装 这里是windows下的安装,另外还有mac和linux下的版本,可自行解决 下载并安装IDEA官网:https://www.jetbrains.com/idea/ 或者百度网盘2017版本 ...
- RocketMQ学习笔记(16)----RocketMQ搭建双主双从(异步复制)集群
1. 修改RocketMQ默认启动端口 由于只有两台机器,部署双主双从需要四个节点,所以只能修改rocketmq的默认启动端口,从官网下载rocketmq的source文件,解压后使用idea打开,全 ...
- DeepCloneObjects 和 DeepClone
ARX AcDbDatabase 中的方法 deepCloneObjects() 和 wblock() 区别以及和 AcDbObject 方法 clone() 和 deepClone() 的关系 Ac ...
- Mybatis学习总结三(动态SQL)
通过mybatis提供的各种标签方法实现动态拼接sql. 一.if 和 where <select id="findUserList" parameterType=" ...
- 关押罪犯 - 并查集&优先队列
题目地址:http://www.51cpc.com/web/problem.php?id=4261 Summarize: 此题最巧妙的是“敌人的敌人就是朋友!”,故需先将敌对关系放入优先队列,按怨恨值 ...
- 60.通过应用层join实现用户与博客的关联
在构造数据模型的时候,将有关联关系的数据分割为不同的实体,类似于关系型数据库中的模型. 案例背景:博客网站,一个网站可能有多个用户,一个用户会发多篇博客,此时最好的方式是建立users和blogs两个 ...