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: ...
随机推荐
- JSP参数传递兼EL表达式
1.浏览器?方式传递参数 /** 浏览器地址栏输入?方式传递参数 ?test=123 */ 可以用${param.test}方式输出 2.页面内部设置参数setAttribute /** JSP页面中 ...
- UEditer的使用
1.首先到官网下载http://ueditor.baidu.com/website/download.html#ueditor 2.然后把解压后的文件复制到项目中(放在UEditer中),如图 3.在 ...
- Apache 和 Nginx 下的 URL 重写
URL 重写和重定向 URL 重写是将页面映射到本站另一页面, 而重定向则是将页面映射到另一主机(域名). 其中临时重定向(R=302)和永久重定向(R=301)都是亲搜索引擎的, 是 SEO 的重要 ...
- java中的数学函数Math方法记录
1,三角函数与属性Math.sin() -- 返回数字的正弦值Math.cos() -- 返回数字的余弦值Math.tan() -- 返回数字的正切值Math.asin() -- 返回数字的反正弦值M ...
- day02 Python完结
一. 常用数据类型及内置法 1 列表 定义: 列表是Python中内置有序.可变序列,列表的所有元素放在一对中括号“[]”中,并使用逗号分隔开: 当列表元素增加或删除时,列表对象自动进行扩展或收缩内存 ...
- 简单了解jdbcTemplate的queryForList是如何查询
queryForList方法会调用query方法,同时会传入一个新的ColumnMapRowMapper对象 ArgumentPreparedStatementSetter对象只有一个Object[] ...
- [Luogu] P4626 一道水题 II
---恢复内容开始--- 题目描述 一天,szb 在上学的路上遇到了灰太狼. 灰太狼:帮我们做出这道题就放了你. szb:什么题? 灰太狼:求一个能被 [1,n] 内所有数整除的最小数字,并对 100 ...
- Flask上下文流程图
如图:
- mess系统 开发技术,需求整理
1.1.WEB开发的相关知识 WEB,在英语中web即表示网页的意思,它用于表示Internet主机上供外界访问的资源. Internet上供外界访问的Web资源分为: 静态web资源(如html 页 ...
- i2c中应答信号信号总结
i2c如果用到主从的关系的时候,需要考虑: give_ack();//从器件发送,来表示占用总线,让sda总线保持低电平. get_ack();//主器件判断是否有器件占用总线,sda有器件占用,是低 ...