LeetCode 451. Sort Characters By Frequency

本文介绍两种高效的字符串频率排序算法:优先队列法和桶排序法。优先队列法利用哈希映射计数,结合优先队列实现字符按频率逆序排列,时间复杂度为O(n+k*logk)。桶排序法则通过建立最大频率对应的桶,直接进行频率排序,时间复杂度为O(n)。
  1. Priority Queue
    时间复杂度应该是O(n + k*lgk),n是输入string的长度,k是string里面unique characters的数量。
    class Solution {
        public String frequencySort(String s) {
            Map<Character, Integer> count = new HashMap<>();
            for(char c: s.toCharArray()){
                count.put(c, count.getOrDefault(c, 0) + 1);
            }
            // Our comparator is (a, b) -> b.length() - a.length().
            // If a is longer than b, then a negative number will be returned
            // telling the sort algorithm to place a first. Otherwise, a positive
            // number will be returned, telling it to place a second.
            // This results in a longest to shortest sorted list of the strings.
            Queue<Character> pq = new PriorityQueue<>((a,b) 
            -> count.get(b) - count.get(a));
            // The keySet() method is used to get a Set view of the keys 
            // contained in this map.
            for(char c: count.keySet()){
                pq.offer(c);
            }
            StringBuilder sb = new StringBuilder();
            while(!pq.isEmpty()){
                char c = pq.poll();
                int copies = count.get(c);
                for(int i = 1; i <= copies; i++){
                    sb.append(c);
                }
            }
            return sb.toString();
        }
    }
    

     

  2. Bucket Sort
    我们用一个maxFreq变量来维护频率的最大值,这样我们只需要建立(maxFreq + 1)个bucket就行了,时间复杂度是O(n)。
    第一种写法:建立List<List<Character>>来承担buckets的作用
    class Solution {
        public String frequencySort(String s) {
            Map<Character, Integer> count = new HashMap<>();
            int maxFreq = 0;
            // Count up the occurances.
            for(char c: s.toCharArray()){
                count.put(c, count.getOrDefault(c, 0) + 1);
                maxFreq = Math.max(maxFreq, count.get(c));
            }
            // Make the list of buckets and apply bucket sort.
            List<List<Character>> buckets = new ArrayList<>();
            // We would like the freqency to become the indices
            // i.e. indices range from 0 to maxFreq
            // hence we need to create a list whose size is maxFreq + 1
            for(int i = 0; i <= maxFreq; i++){
                List<Character> bucket = new ArrayList<>();
                buckets.add(bucket);
            }
            // Add characters to different buckets acoording to their occurences
            for(char c: count.keySet()){
                int freq = count.get(c);
                buckets.get(freq).add(c);
            }
            // Build up the string
            StringBuilder sb = new StringBuilder();
            for(int i = buckets.size() - 1; i >= 1; i--){
                for(char c: buckets.get(i)){
                    for(int j = count.get(c); j >= 1; j--){
                        sb.append(c);
                    }
                }
            }
            return sb.toString();
        }
    }

    第二种写法:创建一个元素为List[]的array。但是有两点要注意,一是创建List[]的语法格式,https://blog.youkuaiyun.com/huanghanqian/article/details/81078799 ; 二是从后往前遍历这个数组的时候(18~26)需要check数组的元素是否为空。因为之前只有往数组里面加char时,我们才会创建对应frequency的ArrayList,否则是null。这是和第一种法所不一样的,第一种写法我们一开始就创建好了所有的List(15~18)。
     

    class Solution {
        public String frequencySort(String s) {
            Map<Character, Integer> count = new HashMap<>();
            int maxFreq = 0;
            for(char c: s.toCharArray()){
                count.put(c, count.getOrDefault(c, 0) + 1);
                maxFreq = Math.max(maxFreq, count.get(c));
            }
            List<Character>[] buckets = new List[maxFreq + 1];
            for(char c: count.keySet()){
                int freq = count.get(c);
                if(buckets[freq] == null){
                    buckets[freq] = new ArrayList<Character>();
                }
                buckets[freq].add(c);
            }
            StringBuilder sb = new StringBuilder();
            for(int i = buckets.length - 1; i > 0; i--){
                if(buckets[i] != null){
                    for(char c: buckets[i]){
                        for(int j = 1; j <= count.get(c); j++){
                            sb.append(c);
                        }
                    }
                } 
            }
            return sb.toString();
        }
    }

     

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值