Leetcode: 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.
- 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.
Bucket Sort + HashMap
- public class Solution {
- public String frequencySort(String s) {
- Map<Character, Integer> map = new HashMap<>();
- for (char c : s.toCharArray()) {
- if (map.containsKey(c)) {
- map.put(c, map.get(c) + 1);
- } else {
- map.put(c, 1);
- }
- }
- List<Character> [] bucket = new List[s.length() + 1];
- for (char key : map.keySet()) {
- int frequency = map.get(key);
- if (bucket[frequency] == null) {
- bucket[frequency] = new ArrayList<>();
- }
- bucket[frequency].add(key);
- }
- StringBuilder sb = new StringBuilder();
- for (int pos = bucket.length - 1; pos >=0; pos--) {
- if (bucket[pos] != null) {
- for (char num : bucket[pos]) {
- for (int i = 0; i < map.get(num); i++) {
- sb.append(num);
- }
- }
- }
- }
- return sb.toString();
- }
- }
HashMap+ Heap+Wrapper Class
- public class Solution {
- public String frequencySort(String s) {
- PriorityQueue<WrapperChar> maxheap = new PriorityQueue(1, new Comparator<WrapperChar>() {
- public int compare(WrapperChar w1, WrapperChar w2) {
- return w2.count - w1.count;
- }
- });
- HashMap<Character, WrapperChar> map = new HashMap<Character, WrapperChar>();
- for (int i=0; i<s.length(); i++) {
- char c = s.charAt(i);
- if (!map.containsKey(c)) map.put(c, new WrapperChar(c, 1));
- else {
- int newCount = map.get(c).count + 1;
- map.put(c, new WrapperChar(c, newCount));
- }
- }
- for (char c : map.keySet()) maxheap.offer(map.get(c));
- StringBuilder res = new StringBuilder();
- while (!maxheap.isEmpty()) {
- WrapperChar wChar = maxheap.poll();
- for (int i=0; i<wChar.count; i++) {
- res.append(wChar.c);
- }
- }
- return res.toString();
- }
- public class WrapperChar {
- char c;
- int count;
- public WrapperChar(char ch, int num) {
- this.c = ch;
- this.count = num;
- }
- }
- }
Leetcode: Sort Characters By Frequency的更多相关文章
- [LeetCode] 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 s, sort it in decreasing order based on the frequency of the characters. The frequenc ...
- #Leetcode# 451. Sort Characters By Frequency
https://leetcode.com/problems/sort-characters-by-frequency/ Given a string, sort it in decreasing or ...
- 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: ...
- 【LeetCode】451. Sort Characters By Frequency 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 优先级队列 排序 日期 题目地址:https: ...
- [Swift]LeetCode451. 根据字符出现频率排序 | 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: Inp ...
- Sort Characters By Frequency
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
随机推荐
- 【BZOJ2038】【2009国家集训队】小Z的袜子(hose) 分块+莫队
Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命……具体来说,小Z把这N只袜 ...
- 给自定义cell赋值代码
// // ViewController.m // 11 - 投资管理 - 李洪强 // // Created by vic fan on 16/4/8. // Copyright © 201 ...
- 1069. The Black Hole of Numbers (20)
For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in ...
- 收集的github的东西
1.voghDev/PdfViewPager-Android widget that can render PDF documents stored on SD card, linked as ass ...
- [LintCode] Letter Combinations of a Phone Number 电话号码的字母组合
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- JAVA双向链表
1.链表是一种重要的数据结构,在程序设计中占有很重要的地位 2.我们可以用类List来实现链表结构,用变量Head.Tail.Length.Pointer来实现表头.存储当前结点的指针时有一定的技 巧 ...
- sendfile传输机制
- 配置RAC到单节点standby的data guard
1RAC主库准备 2创建物理备库 3主库调整参数 4测试DG
- 使用plupload做一个类似qq邮箱附件上传的效果
公司项目中使用的框架是springmvc+hibernate+spring,目前需要做一个类似qq邮箱附件上传的功能,暂时只是上传小类型的附件 处理过程和解决方案都需要添加附件,处理过程和解决方案都可 ...
- iOS 导入第三方文件夹时右侧出现问号
首先,和版本库有关. a代表add,m代表modify,?代表未能识别,通常如果使用git之类的版本控制器,添加文件后没有进行提交,就会出现? 1.遇到引用文件夹为蓝色的情况,是你以为勾了copy项, ...