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.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
不知道怎么存进pq
[英文数据结构或算法,为什么不用别的数据结构或算法]:
只放一个hashmap元素:要用map.entrySet() 用得不多
Map.Entry代表一个哈希表实体
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
Map.Entry中的e.getKey()是不是字母,也不是对象。不用命名,直接存就行了
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
pq中存Map.Entry 代表一个哈希表实体
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
pq类中有类,类中有方法
PriorityQueue<Map.Entry<Character, Integer>> pq = new PriorityQueue<>(
new Comparator<Map.Entry<Character, Integer>>() {
@Override
public int compare(Map.Entry<Character, Integer> a, Map.Entry<Character, Integer> b) {
return b.getValue() - a.getValue();
}
}
);
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public String frequencySort(String s) {
//ini: res map
String res = new String();
Map<Character, Integer> map = new HashMap<>();
//cc
if (s == null || s.length() == 0) return res;
//count char
char[] chars = s.toCharArray();
for (char c : chars) {
if (map.containsKey(c)) {
map.put(c, map.get(c) + 1);
}
else map.put(c, 1);
}
//pq
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();
}
}
);
//append to answer
pq.addAll(map.entrySet());
StringBuilder sb = new StringBuilder();
while (!pq.isEmpty()) {
Map.Entry e = pq.poll();
//char ch = e.getKey();
for (int i = 0; i < (int)e.getValue(); i++) {
sb.append(e.getKey());
}
}
//return new string
return sb.toString();
}
}
451. Sort Characters By Frequency将单词中的字母按照从高频到低频的顺序输出的更多相关文章
- 【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: Input: ...
- 【LeetCode】451. Sort Characters By Frequency 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 优先级队列 排序 日期 题目地址:https: ...
- #Leetcode# 451. Sort Characters By Frequency
https://leetcode.com/problems/sort-characters-by-frequency/ Given a string, sort it in decreasing or ...
- 451. Sort Characters By Frequency (sort map)
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, 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: ...
随机推荐
- MySQL Execution Plan--IN查询计划(2)
在MySQL中,IN查找经常出现性能问题,相同SQL在MySQL不同版本中表现不同. 准备测试数据: ## 创建表tb001 CREATE TABLE tb001( id INT unsigned N ...
- 解决Visual Studio禁止使用strlen函数的问题
问题描述: 在学习C++的复制构造函数以及复制赋值运算符的重载时,需要用到使用C风格的字符串作为引入,由于我用的是VS2015(社区版),在编译时出错.编译器提醒strcpy函数是不安全的,建议改用s ...
- type=number 的maxlength和可以输入E的问题
有一个输入框,要求只能输入1到999以下数字,于是 设置<input type="number" min="1" max="999"& ...
- TF(1): 基础理论
TensorFlow最初由Google大脑的研究员和工程师开发出来,用于机器学习和神经网络方面的研究,于2015.10宣布开源,在众多深度学习框架中脱颖而出,在Github上获得了最多的Star量.T ...
- ubuntu 16.04 mysql5.7.17 开放远程3306端口
ubuntu 16.04 mysql5.7.17 开放远程3306端口 原创 2017年01月19日 20:33:27 标签: mysql / ubuntu 2644 开启mysql的远程访问权限 默 ...
- Android 开发 框架系列 glide-transformations 图片处理基本使用
首先简单的介绍一下Gilde作用范围.Gilde功能十分强大,它可以实现图片处理.图片本地加载.图片网络加载.位图加载.图片内存缓存.图片磁盘缓存.Gif图片加载.使用简单轻松,轻松的后是它强大的心, ...
- 面试回顾——kafka
关于消息队列的使用场景:https://www.cnblogs.com/linjiqin/p/5720865.html kafka: Topic Kafka将消息种子(Feed)分门别类 每一类的消息 ...
- HDFS 常用命令行:
1. 查看各库的存储大小 hdfs dfs -du -h /user/hive/warehouse 2. 删除HDFS 文件 hdfs dfs -rmr 绝对路径名 例如:hdfs dfs -rmr ...
- HTTP协议规定,客户端的编写
HTTP协议是网络应用层协议,建立在TCP/IP协议基础上.HTTP协议基于客户/服务器模式,客户端主动发出HTTP请求,服务器接收HTTP请求,返回HTTP响应结果.HTTP协议对HTTP请求,以及 ...
- 3Linux常用命令
文件目录管理命令 1.touch touch 文件名 #创建空白文件 -a 修改读取(访问)时间atime -m 修改修改时间mtime -d 同时修改atime 和 mtime touch ...