LeetCode 451. Sort Characters By Frequency (根据字符出现频率排序)

时间:2021-11-20 10:13:37

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 (根据字符出现频率排序)的更多相关文章

  1. &lbrack;LeetCode&rsqb; 451&period; Sort Characters By Frequency 根据字符出现频率排序

    Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...

  2. 451 Sort Characters By Frequency 根据字符出现频率排序

    给定一个字符串,请将字符串里的字符按照出现的频率降序排列.示例 1:输入:"tree"输出:"eert"解释:'e'出现两次,'r'和't'都只出现一次.因此' ...

  3. &lbrack;LeetCode&rsqb; Sort Characters By Frequency 根据字符出现频率排序

    Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...

  4. &num;Leetcode&num; 451&period; Sort Characters By Frequency

    https://leetcode.com/problems/sort-characters-by-frequency/ Given a string, sort it in decreasing or ...

  5. 【leetcode】451&period;&&num;160&semi;Sort Characters By Frequency

    Given a string s, sort it in decreasing order based on the frequency of the characters. The frequenc ...

  6. 【LeetCode】451&period; Sort Characters By Frequency 解题报告(Python & C&plus;&plus;)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 优先级队列 排序 日期 题目地址:https: ...

  7. 451&period; Sort Characters By Frequency

    题目: Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Inp ...

  8. 451&period; Sort Characters By Frequency将单词中的字母按照从高频到低频的顺序输出

    [抄题]: Given a string, sort it in decreasing order based on the frequency of characters. Example 1: I ...

  9. 451&period; Sort Characters By Frequency &lpar;sort map&rpar;

    Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...

随机推荐

  1. bootstrap-table填坑之旅&lt&semi;一&gt&semi;认识bootstrap-table

    应公司需求,改版公司ERP的数据显示样式.由于前期开发的样式是bootstrap,所以选bootstrap-table理所当然(也是因为看了bootstrap-table官网的example功能强大, ...

  2. 攻城狮在路上(贰) Spring(二)--- Spring IoC概念介绍

    一.IoC的概念: IoC(控制反转)是Spring容器的核心.另一种解释是DI(依赖注入),即让调用类对某一个接口的依赖关系由第三方注入,以移除调用类对某一个接口实现类的一览. 定义如此,由此可见, ...

  3. Oracle事务之一:锁和隔离

    Oracle事务之一:锁和隔离 一. 事务概述 事务管理是数据库处理的核心.数据库既要保证用户能并发地执行事务,还要保证数据库的一致性. 当第一条可执行的SQL开始执行,就隐形地开始了一个事务,直到遇 ...

  4. oracle 查询月份差

    select to_char(add_months(trunc(sysdate),-1),'yyyymm') from dual;

  5. poj2375 强连通

    题意:有一个 l * w 大小的滑雪场,每个格子都有一个高度,每个格子可以直接通到上下左右四个格子中高度小于等于自己的格子,现在要建立通道,能够连通任意两个格子,问最少建多少通道能够使所有格子能够互相 ...

  6. 下面程序的输出结果是&lowbar;&lowbar;&lowbar;&lowbar; A&colon;11&comma;10 B&colon;11&comma;11 C&colon;10&comma;10 D&colon;10&comma;11 int x&equals;10&semi; int y&equals;x&plus;&plus;&semi; printf&lpar;&quot&semi;&percnt;d&comma;&percnt;d&quot&semi;&comma;&lpar;x&plus;&plus;&comma;y&rpar;&comma;y&plus;&plus;&rpar;&semi;

    下面程序的输出结果是____ A:11,10 B:11,11 C:10,10 D:10,11 int x=10; int y=x++; printf("%d,%d",(x++,y) ...

  7. JavaScript 进制转换

    //十进制转其他 var x=111; alert(x.toString(8)); alert(x.toString(16)); //其他转十进制 var x='112'; alert(parseIn ...

  8. jsp 或 php 等view之中使用javascript简单处理的使用技巧

    前端人员在jsp,php等后台mvc之的coding之时,前端人员常常需要一些少量的数据处理,直接使用js的方法无疑是开销最小的.使用的方法 使用在标签之中嵌套script标签,使用document. ...

  9. Java流程语句

    流程控制语句 if语句: if语句的执行流程 例子: public class IfDemo01 { public static void main(String[] args) { int x = ...

  10. leetcode 443&period; String Compression

    下面反向遍历,还是正向好. void left(vector<char>& v, bool p(int)) { ; ; ; while (del < max_index) { ...