[LC] 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. Solution 1: pq
class Solution {
public String frequencySort(String s) {
Map<Character, Integer> map = new HashMap<>();
for (Character c : s.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
PriorityQueue<Map.Entry<Character, Integer>> pq = new PriorityQueue<>((a, b) -> b.getValue() - a.getValue());
pq.addAll(map.entrySet());
char[] charArr = new char[s.length()];
int i = 0;
while (i < charArr.length) {
Map.Entry<Character, Integer> entry = pq.poll();
Character cur = entry.getKey();
Integer times = entry.getValue();
int j = 0;
while (j < times) {
charArr[i + j] = cur;
j += 1;
}
i += times;
}
return new String(charArr);
}
}
Solution 2: bucket sort based on Freqency
class Solution {
public String frequencySort(String s) {
Map<Character, Integer> map = new HashMap<>();
for (Character c : s.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
// have a freq buckets from 0 to s.length, 0 is unused
List<Character>[] buckets = new List[s.length() + 1];
for (char c : map.keySet()) {
int times = map.get(c);
if (buckets[times] == null) {
buckets[times] = new LinkedList<>();
}
buckets[times].add(c);
}
StringBuilder sb = new StringBuilder();
for (int i = buckets.length - 1; i >= 0; i--) {
if (buckets[i] != null) {
for (char ch: buckets[i]) {
for (int j = 0; j < i; j++) {
sb.append(ch);
}
}
}
}
return sb.toString();
}
}
[LC] 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: I ...
- 451. Sort Characters By Frequency
题目: Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Inp ...
- #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: ...
- 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: ...
随机推荐
- C 的printf函数
头文件 #include <stdio.h> printf函数是最常用的格式化输出函数,原型为:int printf(char *format,......); printf函数会根据参数 ...
- Spring最基础使用1
1. 导入Spring等jar包 2. 配置文件 applicationContext.xml <?xml version="1.0" encoding="UTF- ...
- 18 12 27 css 盒模型使用 以及相关技巧问题 元素溢出 块元素、内联元素、内联块元素
盒子模型的实际尺寸 盒子的width和height设置的是盒子内容的宽和高,不是盒子本身的宽和高,盒子的真实尺寸计算公式如下: 盒子宽度 = width + padding左右 + border左右 ...
- JSP变量、方法和类的声明,JSP程序片,JSP表达式
参考文章 http://c.biancheng.net/view/1431.html http://c.biancheng.net/view/1433.html http://c.biancheng. ...
- 图数据库ubentu环境neo4j安装
1.下载进入官网下载https://neo4j.com/download-center/#releases 2.设置依赖仓库 wget -O - https://debian.neo4j.org/ne ...
- Django1.11模型类数据库操作
django模型类数据库操作 数据库操作 添加数据 1,创建类对象,属性赋值添加 book= BookInfo(name='jack',pub_date='2010-1-1') book.save() ...
- Physicoochemical|CG content|
NCBI存在的问题: 数据用户的增长 软件开发受限 数据分析缺乏 有些传统束缚,仅用底层语言书写 Pangenome Open gene是随菌株数量增大而增大的gene,Closed gene是随菌株 ...
- java设计模式--六大原则
一.单一职责原则 单一职责原则:就一个类而言,应该仅有一个引起它变化的原因.通俗来说,就是互相不相关的属性和方法不要放在一个类中,就好比之前简单工厂模式中介绍的那样,客户端(Customer)应该与工 ...
- 1. react 基础 简介 及 环境搭建
一.简介 由 Facebook 推出 2013 年 开源 的 函数式编程的 使用人数最多的 前端框架 拥有健全的文档与完善的社区 ( 官网 ) react 16 称为 React Fiber ( 底层 ...
- 阿里云服务器下安装配置phpMyAdmin
1.下载phpMyAdmin wget http://www.phpmyadmin.net/home_page/downloads.php 2.解压下载的文件 tar -zvxf phpMyAdmin ...