-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL692.java
More file actions
65 lines (53 loc) · 1.82 KB
/
L692.java
File metadata and controls
65 lines (53 loc) · 1.82 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
package com.liang.leetcode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @ClassName L692
* @description top-k-frequent-words
* @Author LiaNg
* @Date 2018/11/13
*/
public class L692 {
public static void main(String[] args) {
String[] words = {"i", "love", "leetcode", "i", "love", "coding"};
int k = 2;
L692 l = new L692();
System.out.println(l.topKFrequent(words, k));
}
/**
* 给一非空的单词列表,返回前 k 个出现次数最多的单词。
* 返回的答案应该按单词出现频率由高到低排序。如果不同的单词有相同出现频率,按字母顺序排序。
*/
public List<String> topKFrequent(String[] words, int k) {
Map<String, Integer> wordMap = new HashMap();
List<String> resultList = new ArrayList();
for (String word : words) {
wordMap.put(word, wordMap.getOrDefault(word, 0) + 1);
}
List<String>[] sortList = new List[words.length + 1];
for (String key : wordMap.keySet()) {
int value = wordMap.get(key);
if (sortList[value] == null) {
sortList[value] = new ArrayList<>();
}
sortList[value].add(key);
}
int valueLenght = 0;
for (int i = sortList.length - 1; i >= 0 && k > 0; i--) {
if (sortList[i] != null) {
Collections.sort(sortList[i]);
for (int j = 0; j < sortList[i].size(); j++) {
if (valueLenght == k) {
return resultList;
}
resultList.add(sortList[i].get(j));
valueLenght++;
}
}
}
return resultList;
}
}