zoukankan      html  css  js  c++  java
  • 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

     1 public class Solution {
     2     public String frequencySort(String s) {
     3         Map<Character, Integer> map = new HashMap<>();
     4         for (char c : s.toCharArray()) {
     5             if (map.containsKey(c)) {
     6                 map.put(c, map.get(c) + 1);
     7             } else {
     8                 map.put(c, 1);
     9             }
    10         }
    11         List<Character> [] bucket = new List[s.length() + 1];
    12         for (char key : map.keySet()) {
    13             int frequency = map.get(key);
    14             if (bucket[frequency] == null) {
    15                 bucket[frequency] = new ArrayList<>();
    16             }
    17             bucket[frequency].add(key);
    18         }
    19         StringBuilder sb = new StringBuilder();
    20         for (int pos = bucket.length - 1; pos >=0; pos--) {
    21             if (bucket[pos] != null) {
    22                 for (char num : bucket[pos]) {
    23                     for (int i = 0; i < map.get(num); i++) {
    24                         sb.append(num);
    25                     }
    26                 }
    27             }
    28         }
    29         return sb.toString();
    30     }
    31 }

    HashMap+ Heap+Wrapper Class

     1 public class Solution {
     2     public String frequencySort(String s) {
     3         PriorityQueue<WrapperChar> maxheap = new PriorityQueue(1, new Comparator<WrapperChar>() {
     4             public int compare(WrapperChar w1, WrapperChar w2) {
     5                 return w2.count - w1.count;
     6             }
     7         });
     8         HashMap<Character, WrapperChar> map = new HashMap<Character, WrapperChar>();
     9         for (int i=0; i<s.length(); i++) {
    10             char c = s.charAt(i);
    11             if (!map.containsKey(c)) map.put(c, new WrapperChar(c, 1));
    12             else {
    13                 int newCount = map.get(c).count + 1;
    14                 map.put(c, new WrapperChar(c, newCount));
    15             }
    16         }
    17         for (char c : map.keySet()) maxheap.offer(map.get(c));
    18         StringBuilder res = new StringBuilder();
    19         while (!maxheap.isEmpty()) {
    20             WrapperChar wChar = maxheap.poll();
    21             for (int i=0; i<wChar.count; i++) {
    22                 res.append(wChar.c);
    23             }
    24         }
    25         return res.toString();
    26     }
    27     
    28     public class WrapperChar {
    29         char c;
    30         int count;
    31         public WrapperChar(char ch, int num) {
    32             this.c = ch;
    33             this.count = num;
    34         }
    35     }
    36 }
  • 相关阅读:
    redhat 6.4下PXE+Kickstart无人值守安装操作系统
    ubuntu14.04安装好Hadoo之后接着安装hbase和介绍常用命令
    避坑之Hadoop安装伪分布式(Hadoop3.2.0/Ubuntu14.04 64位)
    kindeditor文件上传设置文件说明为上传文件名(JSP版)
    sqlmap 扫描注入漏洞
    局域网内访问不同网段的主机(转记)
    cmd创建用户开启3389命令
    用python来更改windows开机密码
    代码安全之上传文件
    web渗透(转)
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/6178967.html
Copyright © 2011-2022 走看看