- 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(); } } - 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(); } }
LeetCode 451. Sort Characters By Frequency
最新推荐文章于 2025-12-18 17:00:31 发布
本文介绍两种高效的字符串频率排序算法:优先队列法和桶排序法。优先队列法利用哈希映射计数,结合优先队列实现字符按频率逆序排列,时间复杂度为O(n+k*logk)。桶排序法则通过建立最大频率对应的桶,直接进行频率排序,时间复杂度为O(n)。
851

被折叠的 条评论
为什么被折叠?



