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. [LeetCode] 451. 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. [LeetCode] Sort Characters By Frequency 根据字符出现频率排序

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

  4. #Leetcode# 451. Sort Characters By Frequency

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

  5. 【leetcode】451. 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. Sort Characters By Frequency 解题报告(Python & C++)

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

  7. 451. Sort Characters By Frequency

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

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

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

  9. 451. Sort Characters By Frequency (sort map)

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

随机推荐

  1. Errors reported here must be corrected before the service can be started

    场景: 安装.配置Apache24时候,最后会给出提示,如图:Errors reported here must be corrected before the service can be star ...

  2. JS高级——弹出框的美化

    替换原有的alert方法,window.alert=function(){} https://blog.csdn.net/kirsten_z/article/details/76242286 http ...

  3. CNN:测试一下YoloV3

    项目地址:https://pjreddie.com/darknet/yolo/ mAP提升了不少,在VS上试一把 V3 的权值: https://pjreddie.com/media/files/yo ...

  4. LR中日志参数的设置

    LR中日志参数的设置 1.Run-Time Setting日志参数的设置 在loadrunner的vuser菜单下的Run-Time Setting的General的LOG选项中可以对在执行脚本时Lo ...

  5. 13Oracle Database 存储过程

    Oracle Database 存储过程 触发器相当于java中的事件监听,当某事件发生时激活特定的事件并执行相应的逻辑 DML触发器中包含了三种事件 insert update delete 语法格 ...

  6. 05网页<div></div>块内容

    网页<div></div>块内容 <header>此处为新 header 标签的内容</header> <navigation>此处为新 n ...

  7. HEVC-HM16.9源码学习(1)TEncCu::xCompressCU

    函数入口:Void TEncSlice::compressSlice的m_pcCuEncoder->compressCtu( pCtu );调用xCompressCU( m_ppcBestCU[ ...

  8. 总结struts2 iterator status的用法

    1:#status.odd 是否奇数行 2:#status.count 当前行数 3:#status.index 当前行的序号,从0开始『#status.count=#status.index+1』 ...

  9. UVA - 442 Matrix Chain Multiplication(栈模拟水题+专治自闭)

    题目: 给出一串表示矩阵相乘的字符串,问这字符串中的矩阵相乘中所有元素相乘的次数. 思路: 遍历字符串遇到字母将其表示的矩阵压入栈中,遇到‘)’就将栈中的两个矩阵弹出来,然后计算这两个矩阵的元素相乘的 ...

  10. TestNG设置测试用例执行优先级

    @Test(priority = x)设置测试用例执行优先级.x默认为0,0的优先级最高,0>1>2>3... import org.testng.annotations.Test; ...