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: ...
随机推荐
- PythonStudy——Python 内置函数 Built-in function
内置方法:Python中声明每一个类系统都会加上一些默认内置方法,提供给系统调用该类的对象时使用.比如需要实例化一个对象时,需要调用该类的init方法:使用print去打印一个类时,其实调用的是str ...
- cliipblard.js 实现点击复制
<script src="js/clipboard.min.js"></script> <script type="text/javascr ...
- 光速搭lvs + keepalived + nginx
环境: VIP 192.168.2.224 LVS 192.168.2.217 centos7 nginx1 192.168.2.231 cen ...
- PHP使用文件锁解决高并发问题示例
新建一个.txt文件,文件中什么都不用写. [一].阻塞(等待)模式:(只要有其他进程已经加锁文件,当前进程会一直等其他进程解锁文件) <?php //连接数据库 $con=mysqli_con ...
- 【java】final修饰符介绍
final: 最终,作为一个修饰符特点:1.可以修饰类,函数,变量2.被final修的的类不能被继承.因此类用final修饰可以避免被继承,被子类重写功能.3.被final修饰的方法不可以被重写.4. ...
- [UE4]VR成像原理
一.双眼成像原理 二.3D电影成像原理 模拟人眼.用2个摄像机拍摄,模拟人的左眼和右眼 播放的时候2个投影仪分别同时播放左右摄像机拍摄到内容,观众带上3D眼镜,左眼只能看到左摄像机的内容(过滤右摄像机 ...
- redis学习链接收藏
1.redis命令大全--官网 2.redis命令大全--中文翻译版 3.源码(注释版):redis3.0 4.程序代码:<redis入门指南(第二版)>第五章 5.最新的redis-st ...
- 涂抹mysql笔记-管理mysql服务
-DSYSCONFDIR=/mysql/conf \ 所以在conf下建立my.cnf文件 vi my.cnf [client]port=3306socket=/mysql/conf/mysql.so ...
- 关于CoreData的用法
有些同事觉得CoreData是一个看不懂,理解不清的神秘东东,其实ios的本地数据储存是一个sqlite数据库,一个简易的数据库,而这个CoreData是否支持所有储存的数据呢,显然不是的,站在我的角 ...
- matplotlib初识
Matplotlib 能够创建多数类型的图表,如条形图,散点图,条形图,饼图,堆叠图,3D 图和地图图表. import matplotlib.pyplot as plt plt.plot([,,], ...