-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL451.java
More file actions
85 lines (73 loc) · 2.32 KB
/
L451.java
File metadata and controls
85 lines (73 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.liang.leetcode.heap;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
/**
* @ClassName L451
* @description 根据字符出现频率排序
* @Author LiaNg
* @Date 2019-11-27
*/
public class L451 {
public static void main(String[] args) {
String s = "tree";
L451 l451 = new L451();
System.out.println("l451.frequencySort(s) = " + l451.frequencySort(s));
}
/**
* 给定一个字符串,请将字符串里的字符按照出现的频率降序排列。
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/sort-characters-by-frequency
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/
public String frequencySort(String s) {
Map<Character, Integer> map = new HashMap<>();
for (char c : s.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
PriorityQueue<Character> pq = new PriorityQueue<>((o1, o2) -> map.get(o2) - map.get(o1));
for (Character character : map.keySet()) {
pq.offer(character);
}
StringBuilder result = new StringBuilder();
while (!pq.isEmpty()) {
Character character = pq.poll();
for (int i = 0; i < map.get(character); i++) {
result.append(character);
}
}
return result.toString();
}
/**
* LeetCode 耗时最短解答
*/
public String frequencySort1(String s) {
if (s == null || s.isEmpty()) {
return s;
}
int[] freq = new int[256];
char[] chs = s.toCharArray();
for (char ch : chs) {
freq[ch]++;
}
int[] copy = freq.clone();
Arrays.sort(freq);
char[] rets = new char[chs.length];
int cnt = 0;
for (int i = freq.length - 1; i >= 0; i--) {
if (freq[i] == 0) {
return new String(rets);
}
for (int j = 0; j < copy.length; j++) {
if (copy[j] != freq[i]) {
continue;
}
while (copy[j]-- > 0) {
rets[cnt++] = (char) j;
}
}
}
return new String(rets);
}
}